@selector() in Swift?

后端 未结 23 2589
清酒与你
清酒与你 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:03

    Here's a quick example on how to use the Selector class on Swift:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        var rightButton = UIBarButtonItem(title: "Title", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("method"))
        self.navigationItem.rightBarButtonItem = rightButton
    }
    
    func method() {
        // Something cool here   
    }
    

    Note that if the method passed as a string doesn't work, it will fail at runtime, not compile time, and crash your app. Be careful

提交回复
热议问题