我为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
r方法的错误消息已被释放。我可以直接呼叫
showErrorAlert
吗,如果不能,应该如何避免错误消息的发布? 最佳答案
只需将[TestAlert class]
用作目标而不是TestAlert
。