我正在开发一个应用程序,在这个应用程序中我创建了一个定时器,称为每1秒发出一次声音的方法。
10秒后,它会自动失效,但是在一个按钮单击事件上,我在10秒之前就停止了计时器,但是当我再次来到那个视图时,它会创建两个计时器和调用方法两次,第三次它会调用相同的方法3次。
DecilneTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(MakeSound) userInfo:nil repeats:YES];我已经在按钮单击事件中使此无效。
[DecilneTimer invalidate];
DecilneTimer=nil;如何解决定时器的复制创建?
发布于 2015-05-09 13:24:51
你应该使用:
self.DecilneTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(MakeSound) userInfo:nil repeats:YES];使用self.propertyName = ...,您将使用属性访问器并自动保留NSTimer,这样就不会一次又一次地创建它。你所做的就是直接改变伊瓦尔值
https://stackoverflow.com/questions/30140072
复制相似问题