Objective c 将参数传递给NSTimer调用的方法

Objective c 将参数传递给NSTimer调用的方法,objective-c,cocoa-touch,selector,nstimer,Objective C,Cocoa Touch,Selector,Nstimer,如何将参数传递给NSTimer调用的方法?我的计时器如下所示: [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(updateBusLocation) userInfo:nil repeats:YES]; 我希望能够将字符串传递给updateBusLocation方法。另外,我应该在哪里定义updateBusLocation方法?在我创建计时器的同一个.m文件中 编辑: 其实我还是有问题。我收到错误

如何将参数传递给NSTimer调用的方法?我的计时器如下所示:

[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(updateBusLocation) userInfo:nil repeats:YES];
我希望能够将字符串传递给updateBusLocation方法。另外,我应该在哪里定义updateBusLocation方法?在我创建计时器的同一个.m文件中

编辑:

其实我还是有问题。我收到错误消息:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*-[MapKitDisplayViewController updateBusLocation]:未识别的选择器已发送到实例0x4623600”

这是我的密码:

- (IBAction) showBus {

//do something

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateBusLocation) userInfo:txtFieldData repeats:YES];
[txtFieldData release];
 }


 - (void) updateBusLocation:(NSTimer*)theTimer
 {
      NSLog(@"timer method was called");
      NSString *txtFieldData = [[NSString alloc] initWithString:(NSString*)[theTimer userInfo]];
if(txtFieldData == busNum.text) {
    //do something else
    }
    }
编辑#2:
尽管示例代码运行良好,谢谢您的帮助。

您需要在目标中定义方法。既然您将目标设置为“self”,那么是的,相同的对象需要实现该方法。但是你可以把目标设定为你想要的任何东西

userInfo是一个指针,您可以将其设置为您喜欢的任何对象(或集合),并在计时器启动时将其传递给目标选择器

希望有帮助

编辑:。。。简单示例:

设置计时器:

    NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:2.0 
                              target:self 
                              selector:@selector(handleTimer:) 
                              userInfo:@"someString" repeats:NO];
并在同一类中实现该处理程序(假设将目标设置为“self”):


您可以使用userInfo传递参数:
[NSDictionary Dictionary WithObjectsAndKeys:parameterObj1,@“keyOfParameter1]”

一个简单的例子:

[NSTimer scheduledTimerWithTimeInterval:3.0
                                 target:self
                               selector:@selector(handleTimer:)
                               userInfo:@{@"parameter1": @9}
                                repeats:NO];

- (void)handleTimer:(NSTimer *)timer {
    NSInteger parameter1 = [[[timer userInfo] objectForKey:@"parameter1"] integerValue];
}

对于Swift这样做

例如,您希望使用NSTimer发送UILabel

override func viewDidLoad() {
    super.viewDidLoad()

    var MyLabel = UILabel()
    NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("callMethod:"), userInfo: MyLabel, repeats: false)
}


 func callMethod(timer:NSTimer){

    var MyLabel:UILabel = timer.userInfo as UILabel

}

Swift中的附加示例使用字典文字将参数传递给NSTimer调用的方法:

override func viewDidLoad() {
    super.viewDidLoad()

    let dictionary: [String : AnyObject] = ["first element" : "Jordan",
                                            "second element" : Int(23)]

    NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(0.41),
                                           target: self,
                                           selector: "foo:",
                                           userInfo: dictionary,
                                           repeats: false)
}

func foo(timer: NSTimer) {
        let dictionary: [String : AnyObject] = timer.userInfo! as! [String : AnyObject]
        let firstElement: String = dictionary["first element"] as! String
        let secondElement: Int = dictionary["second element"] as! Int
        print("\(firstElement) - \(secondElement)")
}

这不是对这个问题的直接回答,但因为我在搜索时遇到了这个问题,我需要一些不同的东西,它可能会帮助一些人。我想在helper类中调用一个function,我需要将它传入
UIViewController
,而不是将它与userinfo一起传递,因为userinfo不允许我在其他地方手动调用该函数。我创建了另一个函数,计时器将调用该函数,并从那里调用我感兴趣的函数。有帮助的解决办法

Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(self.timerFired), userInfo: nil, repeats:true);

func timerFired() {

myFunction(controller: self)

}

对于Swift 4.0:

您可以拥有一个带有任何参数的函数,并使用“scheduledTimer”块执行需要重复的代码

func someFunction(param1: Int, param2: String) {

    let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
        print(param1)
        print(param2)
    }
}

完成后,请小心调用timer.invalidate(),以防止它继续运行。

我还是有点迷路了。请您给我举一个计时器调用方法并将参数传递给该方法的例子,好吗?我相信很多人在某个时刻都在想这个问题。谢谢
func someFunction(param1: Int, param2: String) {

    let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
        print(param1)
        print(param2)
    }
}