如何使我需要的一个 ntimer 失效

发布于 2024-12-05 04:23:39 字数 651 浏览 4 评论 0原文

- (void)start{
    NSTimer *mtimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scheduleSomeNSTimer:) userInfo:nil repeats:YES];
}

- (void)scheduleSomeNSTimer:(NSTimer *)timer{
    NSTimer *newtimer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(showAction:) userInfo:nil repeats:NO];
}

- (void)showAction:(NSTimer *)timer{
    NSLog(@"action show!");
}

如果我想使按函数调度的 nstimer 之一无效 - (void)addSomeNSTimer:(NSTimer *)timer

应用程序将重复创建 newtimer ,所以当我需要使这些 newnstimer 之一无效时,如何才能我找到了我需要的对象,

例如:应用程序创建 4 个 nstimers 并循环运行我如何找到其中之一并使之无效

- (void)start{
    NSTimer *mtimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scheduleSomeNSTimer:) userInfo:nil repeats:YES];
}

- (void)scheduleSomeNSTimer:(NSTimer *)timer{
    NSTimer *newtimer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(showAction:) userInfo:nil repeats:NO];
}

- (void)showAction:(NSTimer *)timer{
    NSLog(@"action show!");
}

if i want invalidate one of the nstimer which schedule by function - (void)addSomeNSTimer:(NSTimer *)timer

the application will create newtimer repeatly ,so when i need to invalidate one of these newnstimer ,how can i find the object i need

for example:the application create 4 nstimers and run in loop how can i find one of them and invalidate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

走走停停 2024-12-12 04:23:39

您应该在类中保留对计时器的引用并执行以下操作:

self.myTimer = [NSTimerchedTimerWithTimeInterval:1.0 target:selfselector:@selector(scheduleSomeNSTimer:) userInfo:nil Repeats:YES];

所以每当你想要使其无效时,只需执行:

[self.myTimer invalidate];

You should keep a reference to the timer in your class and do:

self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scheduleSomeNSTimer:) userInfo:nil repeats:YES];

so whenever you want to invalidate it, just do:

[self.myTimer invalidate];

2024-12-12 04:23:39

您可以通过以下方式实现此目的:

根据您的反馈,请查看以下答案。

在 .h 文件中,在数组上声明:

NSMutableArray *arrTimers;

在 .m 文件中,在该数组中创建计时器的位置添加计时器。

NSTimer *mtimer = [[NSTimer alloc] init];

mtimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scheduleSomeNSTimer:) userInfo:nil repeats:YES];

[arrTimers addObject:mtimer];

然后您可以像下面这样使其无效:

NSTimer *myTimer = (NSTimer *)[arrTimers objectAtIndex:1];
if(myTimer != nil)
    {
        [myTimer invalidate];
        myTimer = nil;
    }

我希望它能解决您的问题。


if(timer == mtimer)
{

   if(mtimer != nil)
   {
        [mtimer invalidate];
        mtimer = nil;
   }
}

if(timer == newtimer)
{
    if(newtimer != nil)
    {
        [newtimer invalidate];
        newtimer = nil;
    }
}

干杯。

You can achieve this by below:

As per your feedback, please have a look at below answer.

in .h file, declare on Array:

NSMutableArray *arrTimers;

in .m file, add timer in this array where ever you create the timer.

NSTimer *mtimer = [[NSTimer alloc] init];

mtimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scheduleSomeNSTimer:) userInfo:nil repeats:YES];

[arrTimers addObject:mtimer];

Then you can invalidate it like below:

NSTimer *myTimer = (NSTimer *)[arrTimers objectAtIndex:1];
if(myTimer != nil)
    {
        [myTimer invalidate];
        myTimer = nil;
    }

I hope it will resolve your issue.


if(timer == mtimer)
{

   if(mtimer != nil)
   {
        [mtimer invalidate];
        mtimer = nil;
   }
}

if(timer == newtimer)
{
    if(newtimer != nil)
    {
        [newtimer invalidate];
        newtimer = nil;
    }
}

Cheers.

温暖的光 2024-12-12 04:23:39

To_play 是 NSTimer 对象。

[To_play invalidate];

To_play is NSTimer object.

[To_play invalidate];
泪眸﹌ 2024-12-12 04:23:39

声明一个 NSMutableArray 并在您的 ScheduleSomeNSTimer 方法中将 newtimer 对象添加到该数组中。当您使该数组中的计时器对象无效时,您还需要将其从数组中删除。

Declare a NSMutableArray and in Your scheduleSomeNSTimer method, add the newtimer object into the array. When you invalidate a timer object in this array, you also need to remove it from the array.

所谓喜欢 2024-12-12 04:23:39

您可以在不保留对计时器的引用的情况下完成此操作。请改用标志。

@property(nonatomic, assign) BOOL invalidateTimer; 

定时器相关源码:

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scheduleSomeNSTimer:) userInfo:nil repeats:YES];

- (void)scheduleSomeNSTimer:(NSTimer *)timer
{
   if(YES == invalidateTimer)
   {
     if([timer isValid])
     {
       [timer invalidate];
       timer = nil;
     }
   }
}

You can do it without keeping a reference to the timer. Use a flag instead.

@property(nonatomic, assign) BOOL invalidateTimer; 

Source code related to the Timer:

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scheduleSomeNSTimer:) userInfo:nil repeats:YES];

- (void)scheduleSomeNSTimer:(NSTimer *)timer
{
   if(YES == invalidateTimer)
   {
     if([timer isValid])
     {
       [timer invalidate];
       timer = nil;
     }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文