gpt4 book ai didi

ios - 为什么我的按钮在 Swift Playgrounds 中不起作用?

转载 作者:行者123 更新时间:2023-11-29 05:55:38 25 4
gpt4 key购买 nike

我正在尝试创建一个 Playground ,并且有一个按钮显示“让我们玩吧!”并移动到一个新的 View Controller 中。

我查看了该网站的代码并将其放入我的代码中:

http://lab.dejaworks.com/ios-swift-3-playground-uibutton-action/

这是我的全部代码(就像全部):

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
override func loadView() {

//Introduction



let view = UIView()
view.backgroundColor = .red


//title

func labelCool() {

let label = UILabel()
label.frame = CGRect(x: 50, y: 300, width: 400, height: 100)
label.text = "Add-Add - A Wonderful Game!"
//label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.numberOfLines = 3
label.font = UIFont(name: "HelveticaNeue-Bold", size: 30)

UILabel.animate (withDuration: 10.0, animations:{
label.textColor = .black
})
UILabel.animate(withDuration: 5.0, animations:{
label.textColor = .blue
})
view.addSubview(label)

}

labelCool()

//subtitle
let subtitle = UILabel()
subtitle.frame = CGRect(x: 50, y: 400, width: 200, height: 50)
subtitle.text = "Curated and Created by Yours Truly, Adit Dayal!"
subtitle.numberOfLines = 4
self.view = view
view.addSubview(subtitle)




}

override func viewDidLoad() {
class Responder : NSObject {
@objc func action() {
print("Yay!")
}
}


let responder = Responder()

//next page
let button = UIButton(frame : CGRect(x: 0, y: 500, width: 200, height: 50))
button.setTitle("Let's Play!", for: .normal)
button.backgroundColor = .blue
button.addTarget(responder, action: #selector(Responder.action), for: .touchUpInside)
view.addSubview(button)
}

}

class gameViewController: UIViewController {

}







// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

现在,我只希望按钮显示“Yay!”单击时,但它什么也没做!有谁知道为什么? (我的时间有点有限)

非常感谢,

阿迪特·达亚尔

最佳答案

您的 Responder 类位于 viewDidLoad() 函数内部,将类放在外面,如下所示

class Responder : NSObject {
@objc func action() {
print("Yay!")
}
}

override func viewDidLoad() {
let responder = Responder()

//next page
let button = UIButton(frame : CGRect(x: 0, y: 500, width: 200, height: 50))
button.setTitle("Let's Play!", for: .normal)
button.backgroundColor = .blue
button.addTarget(responder, action: #selector(responder.action), for: .touchUpInside)
view.addSubview(button)
}

关于ios - 为什么我的按钮在 Swift Playgrounds 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55193352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com