Passing parameters to the method called by a NSTimer

前端 未结 6 1081
我寻月下人不归
我寻月下人不归 2020-12-02 09:08

How can I pass a parameter to the method called by a NSTimer? My timer looks like this:

[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selec         


        
6条回答
  •  遥遥无期
    2020-12-02 09:45

    Not a direct answer to the question but since i ran into this while searching and i needed something different it may help someone. I wanted to call a funciton in a helper class, that i needed to pass in the UIViewController, rather than passing it with the userinfo which would not allow me to call the function manually elsewhere i created another function which the timer would call and called the function that i was interested in from there. A workaround that helped.

    Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(self.timerFired), userInfo: nil, repeats:true);
    
    func timerFired() {
    
    myFunction(controller: self)
    
    }
    

提交回复
热议问题