@selector() in Swift?

后端 未结 23 2566
清酒与你
清酒与你 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条回答
  •  暖寄归人
    2020-11-21 16:14

    It may be useful to note where you setup the control that triggers the action matters.

    For example, I have found that when setting up a UIBarButtonItem, I had to create the button within viewDidLoad or else I would get an unrecognized selector exception.

    override func viewDidLoad() {
        super.viewDidLoad() 
    
        // add button
        let addButton = UIBarButtonItem(image: UIImage(named: "746-plus-circle.png"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("addAction:"))
        self.navigationItem.rightBarButtonItem = addButton
    }
    
    func addAction(send: AnyObject?) {     
        NSLog("addAction")
    }
    

提交回复
热议问题