在ScheduledTimerWithTimeInterval中调用类级别的方法 [英] call a class level method in scheduledTimerWithTimeInterval

查看:73
本文介绍了在ScheduledTimerWithTimeInterval中调用类级别的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Alert创建了一个类级方法:

I make a class level method for 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 中调用它,例如:

and I want to call it directly in scheduledTimerWithTimeInterval like:

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

当然有语法错误.

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

I know I can put showErrorAlert to a method:

- (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方法中的错误消息.

But it will cause crash when showErrorAlert is called, because error message from showError method has been released.

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

Can i call showErrorAlert directly, If I can't, how should I avoid error message's release ?

推荐答案

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

Just use [TestAlert class] as a target instead of TestAlert.

这篇关于在ScheduledTimerWithTimeInterval中调用类级别的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆