@selector() in Swift?

后端 未结 23 2544
清酒与你
清酒与你 2020-11-21 15:24

I\'m trying to create an NSTimer in Swift but I\'m having some trouble.

NSTimer(timeInterval: 1, target: self, selector: test(), us         


        
23条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 16:15

    Also, if your (Swift) class does not descend from an Objective-C class, then you must have a colon at the end of the target method name string and you must use the @objc property with your target method e.g.

    var rightButton = UIBarButtonItem(title: "Title", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("method"))
    
    @objc func method() {
        // Something cool here   
    } 
    

    otherwise you will get a "Unrecognised Selector" error at runtime.

提交回复
热议问题