我正在开发一个 iOS 应用程序(固件:5.0+ 和 ARC),它需要每秒更新。
目前,我在一个方法中使用了此方法(当 -ViewDidLoad 时在 PerformSelector 中调用该方法):
-(void)FireOnload {
counter = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(countDownTime) userInfo:nil repeats:YES];
[counter fire];
[[NSRunLoop mainRunLoop] addTimer:counter forMode: NSDefaultRunLoopMode]; // <-- Causing Problem
}
-(void)countDownTime
{
double timeNow = [[NSDate date] timeIntervalSince1970];
double timeLeft = timeEnding - timeNow;
if(timeLeft < 60) // Less then 60 seconds, do something.
{
//Do Stuff
}
}
counter
变量在 header 中被称为 NSTimer。
我显式调用 [counter fire]
,因此 View 加载后就会调用它,而不是 1 秒后调用。
这在 iOS 模拟器中工作正常并且每秒都会触发,但是当它进入 iDevice 时,它就会崩溃。
我已经注释掉了 NSRunLoop
行,并且 iDevice 不会崩溃。但是,它不再每秒更新。
我在这里做错了什么?
最佳答案
发现问题:
更改:
@property(弱,非原子)NSTimer *counter;
至:
@property(强,非原子)NSTimer *counter;
如果您不使用 ARC,则需要将 strong
替换为 retain
关于objective-c - NSRunLoop 在模拟器上工作正常,但 iDevice 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9882311/