嗨,提前感谢。我有一个问题,当我NSTimer
在一个视图中启动一个,然后用一个切换到另一个视图时UIPickerView
,它UIPickerView
的行为会受到影响。我在任何视图和包含 的视图之间来回切换NSTimer
的次数越多, 的行为受到的影响就越大——它们滞后并且移动缓慢——最终达到不调用该方法UIPickerView
的地步。受到影响的不仅仅是一个,而是我的应用程序中的所有内容。UIPickerView
DidSelectRow
UIPickerView
UIPickerView
如果我决定不首先激活NSTimer
,我的UIPickerViews
工作没有问题。但是,当我调用这些NSTimer
方法时,在包含 和任何其他视图的视图之间切换回来和第四NSTimer
次可能六次之后,我的应用程序UIPickerView
都完全停止工作。为了恢复它们的正确行为,我需要关闭应用程序并重新启动它。
我正在使用ARC
,所以我没有NSTimer
手动发布 - 但我认为这与我的问题有关。我猜NSTimer
每次我切换回包含NSTimer
. 无论如何,这是我编码任何东西的第二次努力,所以我不确定如何解决这个问题,尽管我已经读过NSTimer
andUIPickerView
可以通过相同的NSRunLoop
or 线程分配,但我不确定这意味着什么。
无论如何,这是我的代码 - 它非常通用的样板代码。
-(void)showActivity:(NSTimer *)tim {
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss.S"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString* timeString = [dateFormatter stringFromDate:timerDate];
stopWatchLabel.text = timeString;
}
- (IBAction)onStartPressed:(UIButton *)sender; {
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1/10
target:self
selector:@selector(showActivity:)
userInfo:nil
repeats:YES];
// Save the new start date every time
startDate = [[NSDate alloc] init]; // equivalent to [[NSDate date] retain];
NSDate *savedMentionDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"mostRecentMentionDate"];
if (savedMentionDate == nil) {
//There is no existing mention, so save the most recent one
[[NSUserDefaults standardUserDefaults]setObject:startDate forKey:@"mostRecentMentionDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
startDate = savedMentionDate;
}
[stopWatchTimer fire];
timerSetting = 0;
NSNumber* timerSettingNS = [[NSNumber alloc] initWithInt:timerSetting];
[[NSUserDefaults standardUserDefaults] setObject:timerSettingNS forKey:@"timerSetting"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (IBAction)onStopPressed:(UIButton *)sender {
[stopWatchTimer invalidate];
}
- (IBAction)resetTimer:(UIButton *)sender; {
stopWatchLabel.text = @"00:00:00.0";
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"mostRecentMentionDate"];
timerSetting = 1;
NSNumber* timerSettingNS = [[NSNumber alloc] initWithInt:timerSetting];
[[NSUserDefaults standardUserDefaults] setObject:timerSettingNS forKey:@"timerSetting"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
-(void)ViewDidLoad {
...
...
NSNumber* timerSettings = [defaults objectForKey:@"timerSetting"];
if (timerSettings == nil || timerSettings.intValue == 1) {
[self resetTimer:resetTime];
} else if (timerSettings.intValue == 0) {
[self onStartPressed:start];
}
}