@selector() in Swift?

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

    I found many of these answers to be helpful but it wasn't clear how to do this with something that wasn't a button. I was adding a gesture recognizer to a UILabel in swift and struggled so here's what I found worked for me after reading everything above:

    let tapRecognizer = UITapGestureRecognizer(
                target: self,
                action: "labelTapped:")
    

    Where the "Selector" was declared as:

    func labelTapped(sender: UILabel) { }
    

    Note that it is public and that I am not using the Selector() syntax but it is possible to do this as well.

    let tapRecognizer = UITapGestureRecognizer(
                target: self,
                action: Selector("labelTapped:"))
    

提交回复
热议问题