Ios 如何通过重复停止计时器:是
我有一个“段控制”分配给一个有2个定时器的函数 一个重复:否,一个重复:是 我想,当我的区段处于位置“0”时,计时器处于打开状态,重复:否 当我的网段处于位置“1”时,计时器打开,重复:是 但是如果我把我的片段放在位置“1”,我的计时器就永远不会停止 我的职能:Ios 如何通过重复停止计时器:是,ios,timer,Ios,Timer,我有一个“段控制”分配给一个有2个定时器的函数 一个重复:否,一个重复:是 我想,当我的区段处于位置“0”时,计时器处于打开状态,重复:否 当我的网段处于位置“1”时,计时器打开,重复:是 但是如果我把我的片段放在位置“1”,我的计时器就永远不会停止 我的职能: -(IBAction)sendFocusLiveBtn{ if(liveManual.selectedSegmentIndex == 0){ [NSTimer scheduledTimerWithTimeInterval:0.1
-(IBAction)sendFocusLiveBtn{
if(liveManual.selectedSegmentIndex == 0){
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sendFocusLive) userInfo:nil repeats:NO];
}
if (liveManual.selectedSegmentIndex == 1){
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sendFocusLive) userInfo:nil repeats:YES];
}
}
如何阻止它
提前感谢您,而不是做:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sendFocusLive) userInfo:nil repeats:YES];
本身,将计时器保存在属性(或ivar)中,如下所示:
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sendFocusLive) userInfo:nil repeats:YES];
然后你可以做:
[self.timer invalidate];
无论何时何地,您都可以真正停止计时器。而不是执行以下操作:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sendFocusLive) userInfo:nil repeats:YES];
本身,将计时器保存在属性(或ivar)中,如下所示:
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sendFocusLive) userInfo:nil repeats:YES];
然后你可以做:
[self.timer invalidate];
无论何时何地,您都可以真正停止计时器。而不是执行以下操作:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sendFocusLive) userInfo:nil repeats:YES];
本身,将计时器保存在属性(或ivar)中,如下所示:
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sendFocusLive) userInfo:nil repeats:YES];
然后你可以做:
[self.timer invalidate];
无论何时何地,您都可以真正停止计时器。而不是执行以下操作:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sendFocusLive) userInfo:nil repeats:YES];
本身,将计时器保存在属性(或ivar)中,如下所示:
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sendFocusLive) userInfo:nil repeats:YES];
然后你可以做:
[self.timer invalidate];
无论何时何地,只要您准备好真正停止计时器。除了@MichaelDautermann的答案,您应该将选择器更改为
sendFocusLive:
(注意末尾的冒号),并声明该选择器为:
- (void)sendFocusLive:(NSTimer*)timer
{
// Here you can test that timer == self.timer to ensure the right timer has triggered this method
// And you can call [timer invalidate] here when required
// It's a good idea to set timer to nil after you invalidate it
}
除了@MichaelDautermann的答案外,您还应该将选择器更改为
sendFocusLive:
(注意末尾的冒号),并将该选择器声明为:
- (void)sendFocusLive:(NSTimer*)timer
{
// Here you can test that timer == self.timer to ensure the right timer has triggered this method
// And you can call [timer invalidate] here when required
// It's a good idea to set timer to nil after you invalidate it
}
除了@MichaelDautermann的答案外,您还应该将选择器更改为
sendFocusLive:
(注意末尾的冒号),并将该选择器声明为:
- (void)sendFocusLive:(NSTimer*)timer
{
// Here you can test that timer == self.timer to ensure the right timer has triggered this method
// And you can call [timer invalidate] here when required
// It's a good idea to set timer to nil after you invalidate it
}
除了@MichaelDautermann的答案外,您还应该将选择器更改为
sendFocusLive:
(注意末尾的冒号),并将该选择器声明为:
- (void)sendFocusLive:(NSTimer*)timer
{
// Here you can test that timer == self.timer to ensure the right timer has triggered this method
// And you can call [timer invalidate] here when required
// It's a good idea to set timer to nil after you invalidate it
}
如果不再使用计时器,请将计时器属性设置为nil。[自动定时器失效];self.timer=nil;如果不再使用计时器,请将计时器属性设置为nil。[自动定时器失效];self.timer=nil;如果不再使用计时器,请将计时器属性设置为nil。[自动定时器失效];self.timer=nil;如果不再使用计时器,请将计时器属性设置为nil。[自动定时器失效];self.timer=nil;