如何将参数传递给NSTimer调用的方法?我的计时器看起来像这样:
[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(updateBusLocation) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
我希望能够将字符串传递给方法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
}
}
Run Code Online (Sandbox Code Playgroud)
编辑#2:没关系,您的示例代码可以正常工作,感谢您的帮助.
我想知道是否有人知道MKAnnotationView类的任何子类.在苹果文档中,他们说一个例子是MKPinAnnotationView,所以我想知道是否有其他预先创建的子类,如用于跟踪设备当前位置的子类.如果有人有关于创建我自己的MKAnnotationView类的子类的提示,那也很棒.
谢谢,bubster
这行代码有什么作用?
len = ( s.length>t.length ) ? s.length : t.length
Run Code Online (Sandbox Code Playgroud) 由于某种原因,我传递给我的if语句的参数不正确,即使它应该是我的代码:
if (currentAttribute == cBusName)
{
NSLog(@"currentAttribute == cBusName");
}
Run Code Online (Sandbox Code Playgroud)
当前属性和cBusName都是NSMutableStrings两者都等于"1"但NSLog永远不会输出控制台中的字符串是否有我缺少的东西???