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

时间:2019-03-16 04:05:55

标签: ios swift uibutton swift-playground

我正在尝试建立一个操场,并且有一个按钮,上面写着“让我们开始吧!”并移至新的视图控制器。

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

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()

现在,我只希望按钮显示“是!”当单击时,它什么都不做! 有人知道为什么吗? (我受时间限制)

非常感谢您

Adit Dayal

2 个答案:

答案 0 :(得分:0)

您的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)
}

答案 1 :(得分:0)

问题在于您正在viewDidLoad内将响应者对象创建为局部变量;这会导致该对象在函数结束时被破坏(但我们希望该对象即使在此之后都还活着)。您必须保留该对象,因此无需创建局部变量,只需将其保存为类作用域即可创建实例变量:

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

let responder = Responder() // this is now outside the viewDidLoad, so it's an instance variable

override func viewDidLoad() {
    //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)
}