【发布时间】:2011-04-30 00:07:03
【问题描述】:
如何将参数传递给 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: 没关系,您的示例代码可以正常工作,感谢您的帮助。
【问题讨论】:
-
我确定很多人都曾在某个时间点想知道的可靠问题。谢谢!
标签: objective-c cocoa-touch selector nstimer