当我通过xcode构建我的应用程序时,当我按下与starttimer链接的按钮时,它总是崩溃:它突出显示了代码中的最后一个花括号。重要的是要注意,当我通过模拟器启动应用程序时,我可以单击按钮而不会崩溃。发生了什么?这是一些代码:
@implementation TimeController
int timeTick = 0;
NSTimer *timer;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
labelTime.text = @"0";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)startTimer:(id)sender {
[timer invalidate];
timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:(self) selector:(@selector(tick)) userInfo:(nil) repeats:(YES)];
}
- (IBAction)resetTicktock:(id)sender {
[timer invalidate];
timeTick=0;
labelTime.text = @"0";
}
-(void)tick{
timeTick++;
NSString *timeString = [[NSString alloc] initWithFormat:@"%d", timeTick];
labelTime.text = timeString;
}
@end
提前致谢!
答案 0 :(得分:2)
调用方法时不需要()。
而不是
timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:(self) selector:(@selector(tick)) userInfo:(nil) repeats:(YES)];
你应该
timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick) userInfo:nil repeats:YES];