在我的项目中实现之前,我已经创建了一个带有计时器的测试应用程序。
这是我第一次使用计时器。
但问题是当我使用计时器实现时[NSTimer timerWithTimeInterval: target: selector: userInfo: repeats: ];
,它不起作用。
这是我的代码,
界面:
@interface uialertViewController : UIViewController
{
NSTimer *timer;
}
-(void)displayAlert;
-(void)hideandview;
@end
执行:
@implementation uialertViewController
- (void)viewDidLoad {
[self displayAlert];
[super viewDidLoad];
}
-(void)displayAlert{
timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(hideandview) userInfo:nil repeats:NO];
alert = [[UIAlertView alloc] initWithTitle:@"testing" message:@"hi hi hi" delegate:nil cancelButtonTitle:@"continue" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
}
-(void)hideandview{
NSLog(@"triggered");
[alert dismissWithClickedButtonIndex:0 animated:YES];
[alert release];
[self displayAlert];
}
@end
然后我改变了 [NSTimer timerWithTimeInterval: target: selector: userInfo: repeats: ];
with [NSTimer scheduledTimerWithTimeInterval: target: selector:userInfo: repeats: ];
, 这是工作。出了什么问题timerWithTimeInterval:
?我在第一次实施中遗漏了什么吗?提前致谢。
scheduledTimerWithTimeInterval:invocation:repeats:
and scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
创建自动添加到NSRunLoop
,这意味着您不必自己添加它们。将它们添加到NSRunLoop
是导致它们着火的原因。
With timerWithTimeInterval:invocation:repeats:
and timerWithTimeInterval:target:selector:userInfo:repeats:
,您必须手动将计时器添加到运行循环中,代码如下:
[[NSRunLoop mainRunLoop] addTimer:repeatingTimer forMode:NSDefaultRunLoopMode];
这里的其他答案建议您需要致电fire
你自己。你不需要——一旦计时器进入运行循环,它就会被调用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)