如何将参数传递给NSTimer调用的方法?我的计时器如下所示:
[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(updateBusLocation) userInfo:nil repeats:YES];
我希望能够将字符串传递给updateBusLocation方法.另外,我应该在哪里定义updateBusLocation方法?同样的.我创建计时器的m文件?
EDIT:
其实我还是有问题.我收到了错误信息:
由于未捕获的异常"NSInvalidArgumentException"而终止应用程序,原因:"*-[MapKitDisplayViewController updateBusLocation]:未识别的 Select 器已发送到实例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: