当我按下停止按钮停止计时器时,它会重置为原始时间并重新开始倒计时。我到处寻找,我发现的所有东西都是“无效的”,它不起作用。我想停下来的时候停下来,标签上显示原来的时间。我也关掉自动计数,所以我可以尝试释放,它是给我的错误:如何停止NSTimer
0x10e20a5: movl 16(%edx), %edx EXC_BAD_ACCESS (code=2, address=0x10)
NSTimer *rockettTimer;
int rocketCount;
@interface FirstViewController()
@property (strong, nonatomic) IBOutlet UILabel *rocketTimer;
- (IBAction)stopButton:(id)sender;
- (IBAction)startButton:(id)sender;
@end
@implementation FirstViewController
@synthesize rocketTimer;
-(void) rocketTimerRun{
rocketCount = rocketCount - 1;
int minuts = rocketCount/60;
int seconds = rocketCount - (minuts * 60);
NSString *timerOutput = [NSString stringWithFormat:@"%d:%.2d", minuts, seconds];
rocketTimer.text = timerOutput;
}
- (IBAction)startButton:(id)sender {
rocketCount = 180;
rockettTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(rocketTimerRun) userInfo:nil repeats:YES];
- (IBAction)stopButton:(id)sender {
[rockettTimer invalidate];
//[rockettTimer release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setRocketTimer:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
@end
你确定你的stopButton连接到stopButton而不是错误的startButton吗?还要注意,invalidate释放定时器,所以不要调用release和invalidate。 – rmaddy
您可能希望仔细查看代码,没有什么能够防止用户多次按下启动按钮,并创建多个定时器,然后再继续参加...因此BAD_ACCESS –