以下方法导致崩溃。用户界面就像一个按钮,它处理NSTimer的开始/停止功能。如果定时器运行,则更新UILabel。使用viewDidLoad方法使我的计时器工作,停止工作,但再次启动会崩溃应用程序。NSTimer导致崩溃
删除viewDidLoad方法中的alloc并尝试使用开始按钮导致崩溃立即。即使NSLog(@"Start now");
未被调用。
代码:
- (void)tick {
NSLog(@"tick");
float value = [moneyLabel.text floatValue];
moneyLabel.text = [NSString stringWithFormat:@"%f", value + 1.0];
}
- (IBAction)startStopButtonClicked:(UIButton *)sender {
if ([sender.titleLabel.text isEqualToString:@"Start"]) {
NSLog(@"Start now");
if (timer) {
NSLog(@"Timer valid");
[timer fire];
} else {
NSLog(@"Timer is nil");
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tick) userInfo:nil repeats:YES];
[timer fire];
}
NSLog(@"bla");
[sender setTitle:@"Stop" forState:UIControlStateNormal];
} else {
[timer invalidate];
timer = nil;
NSLog(@"Stopped.");
NSLog(@"Timer isValid: %@", timer);
[sender setTitle:@"Start" forState:UIControlStateNormal];
}
}
发布您的崩溃日志。 – Devang
是PLZ发表您的崩溃日志,以便我们可以帮助你... – sandy
***终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因是:“ - [__ NSCFArray startStopButtonClicked:]:无法识别的选择发送到实例0x683d8e0” – DAS