How to use addTarget method in swift 3

后端 未结 10 1275
清酒与你
清酒与你 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:41

    In swift 3 use this -

    object?.addTarget(objectWhichHasMethod, action: #selector(classWhichHasMethod.yourMethod), for: someUIControlEvents)
    

    For example(from my code) -

    self.datePicker?.addTarget(self, action:#selector(InfoTableViewCell.datePickerValueChanged), for: .valueChanged)
    

    Just give a : after method name if you want the sender as parameter.

    0 讨论(0)
  • 2020-11-29 02:44

    Try this with Swift 4

    buttonSection.addTarget(self, action: #selector(actionWithParam(_:)), for: .touchUpInside)
    @objc func actionWithParam(sender: UIButton){
        //...
    }
    
    buttonSection.addTarget(self, action: #selector(actionWithoutParam), for: .touchUpInside)
    @objc func actionWithoutParam(){
        //...
    }
    
    0 讨论(0)
  • 2020-11-29 02:47

    Try this

    button.addTarget(self, action:#selector(handleRegister()), for: .touchUpInside). 
    

    Just add parenthesis with name of method.

    Also you can refer link : Value of type 'CustomButton' has no member 'touchDown'

    0 讨论(0)
  • 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)
        }
    }
    
    0 讨论(0)
  • 2020-11-29 02:49

    The poster's second comment from September 21st is spot on. For those who may be coming to this thread later with the same problem as the poster, here is a brief explanation. The other answers are good to keep in mind, but do not address the common issue encountered by this code.

    In Swift, declarations made with the let keyword are constants. Of course if you were going to add items to an array, the array can't be declared as a constant, but a segmented control should be fine, right?! Not if you reference the completed segmented control in its declaration.

    Referencing the object (in this case a UISegmentedControl, but this also happens with UIButton) in its declaration when you say .addTarget and let the target be self, things crash. Why? Because self is in the midst of being defined. But we do want to define behaviour as part of the object... Declare it lazily as a variable with var. The lazy fools the compiler into thinking that self is well defined - it silences your compiler from caring at the time of declaration. Lazily declared variables don't get set until they are first called. So in this situation, lazy lets you use the notion of self without issue while you set up the object, and then when your object gets a .touchUpInside or .valueChanged or whatever your 3rd argument is in your .addTarget(), THEN it calls on the notion of self, which at that point is fully established and totally prepared to be a valid target. So it lets you be lazy in declaring your variable. In cases like these, I think they could give us a keyword like necessary, but it is generally seen as a lazy, sloppy practice and you don't want to use it all over your code, though it may have its place in this sort of situation. What it

    There is no lazy let in Swift (no lazy for constants).

    Here is the Apple documentation on lazy.

    Here is the Apple on variables and constants. There is a little more in their Language Reference under Declarations.

    0 讨论(0)
  • 2020-11-29 02:55

    Instead of

    let loginRegisterButton:UIButton = {
    //...  }()
    

    Try:

    lazy var loginRegisterButton:UIButton = {
    //...  }()
    

    That should fix the compile error!!!

    0 讨论(0)
提交回复
热议问题