@selector() in Swift?

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

    For swift 3

    let timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(self.test), userInfo: nil, repeats: true)
    

    Function declaration In same class:

    @objc func test()
    {
        // my function
    } 
    
    0 讨论(0)
  • 2020-11-21 16:09

    For Swift 3

    //Sample code to create timer

    Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(updateTimer)), userInfo: nil, repeats: true)
    
    WHERE
    timeInterval:- Interval in which timer should fire like 1s, 10s, 100s etc. [Its value is in secs]
    target:- function which pointed to class. So here I am pointing to current class.
    selector:- function that will execute when timer fires.
    
    func updateTimer(){
        //Implemetation 
    } 
    
    repeats:- true/false specifies that timer should call again n again.
    
    0 讨论(0)
  • 2020-11-21 16:10

    Change as a simple string naming in the method calling for selector syntax

    var timer1 : NSTimer? = nil
    timer1= NSTimer(timeInterval: 0.1, target: self, selector: Selector("test"), userInfo: nil, repeats: true)
    

    After that, type func test().

    0 讨论(0)
  • 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:"))
    
    0 讨论(0)
  • 2020-11-21 16:11

    Selector in Swift 4:

    button.addTarget(self, action: #selector(buttonTapped(sender:)), for: UIControlEvents.touchUpInside)
    
    0 讨论(0)
  • 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")
    }
    
    0 讨论(0)
提交回复
热议问题