How to use addTarget method in swift 3

后端 未结 10 1398
清酒与你
清酒与你 2020-11-29 02:20

here is my button object

    let loginRegisterButton:UIButton = {
    let button = UIButton(type: .system)
    button.backgroundColor = UIColor         


        
10条回答
  •  醉话见心
    2020-11-29 02:47

    the Demo from Apple document. https://developer.apple.com/documentation/swift/using_objective-c_runtime_features_in_swift

    import UIKit
    class MyViewController: UIViewController {
        let myButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
    
        override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
            super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
            // without parameter style
            let action = #selector(MyViewController.tappedButton)
            // with parameter style
            // #selector(MyViewController.tappedButton(_:))
            myButton.addTarget(self, action: action, forControlEvents: .touchUpInside)
        }
    
        @objc func tappedButton(_ sender: UIButton?) {
            print("tapped button")
        }
    
        required init?(coder: NSCoder) {
            super.init(coder: coder)
        }
    }
    

提交回复
热议问题