在scheduledTimerWithTimeInterval中调用类级方法

时间:2012-06-15 03:44:41

标签: iphone ios nstimer

我为Alert提供了一个类级方法:

@interface TestAlert
@end
+ (void)showErrorAlert:(NSTimer *)message
{
.......
 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:messageIn delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
}

我希望直接在scheduledTimerWithTimeInterval中调用它:

    [NSTimer scheduledTimerWithTimeInterval:0.001 target:TestAlert selector:@selector( showErrorAlert:) userInfo:error repeats:NO];

当然有语法错误。

我知道我可以将showErrorAlert放到方法中:

- (void)showError:(NSTimer *)timer
{
    //NSLog(@"show error %@", error);
    [TestAlert showErrorAlert:(NSString *)[timer userInfo]];
}

然后

[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(showError:) userInfo:error1 repeats:NO];

但调用showErrorAlert时会导致崩溃,因为showErro方法的错误消息已被释放。

我可以直接拨打showErrorAlert吗?如果我不能,我应该如何避免错误信息的发布?

2 个答案:

答案 0 :(得分:2)

只需使用[TestAlert class]作为目标,而不是TestAlert

答案 1 :(得分:0)

尝试这个怎么样? 你可以在这里找到所有类型的performSelector:方法:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html

  • performSelector:
  • performSelector:withObject:
  • performSelector:withObject:withObject: - performSelector:withObject:afterDelay:
  • performSelector:withObject:afterDelay:inModes:
  • performSelectorOnMainThread:withObject:waitUntilDone:
  • performSelectorOnMainThread:withObject:waitUntilDone:模式:
  • performSelector:onThread:withObject:waitUntilDone:
  • performSelector:onThread:withObject:waitUntilDone:modes:
  • performSelectorInBackground:withObject: