Ios NSTimer不';不要停止与无效

Ios NSTimer不';不要停止与无效,ios,nstimer,invalidation,Ios,Nstimer,Invalidation,我添加了这样的计时器 tim=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(repeatTim) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:tim forMode:NSDefaultRunLoopMode]; tim这是我的班级的财产 然后我按一下按钮就停止了 [[fbt tim] invalidate]; [fbt

我添加了这样的计时器

tim=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(repeatTim) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:tim forMode:NSDefaultRunLoopMode];
tim这是我的班级的财产

然后我按一下按钮就停止了

[[fbt tim] invalidate];
[fbt setTim:nil];
fbt它是我的类的实例

若我只调用invalidate,那个么它不会停止,但若我将它设置为nil,那个么我就得到了EXC_断点

这里是选择器中repeatTim方法的代码

AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
[appDelegate.wbv stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"intal()"]];
我试图在中调用initinvalidate

dispatch_async(dispatch_get_main_queue(), ^{})

它也不会停止计时器。

请阅读NSTimer的文档:

有三种方法可以创建计时器:

  • 使用scheduledTimerWithTimeInterval:invocation:repeats:或scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:class方法创建计时器,并在默认模式下在当前运行循环上对其进行调度

  • 使用timerWithTimeInterval:invocation:repeats:或timerWithTimeInterval:target:selector:userInfo:repeats:class方法创建计时器对象,而无需在运行循环上对其进行调度。(创建计时器后,必须通过调用相应nsrunlop对象的addTimer:forMode:method手动将计时器添加到运行循环中。)

  • 分配计时器并使用initWithFireDate:interval:target:selector:userInfo:repeats:method初始化它。(创建计时器后,必须通过调用相应nsrunlop对象的addTimer:forMode:method手动将计时器添加到运行循环中。)

  • 您正在使用的方法已将其从1添加到mainLoop。-您需要删除此行或创建一个带有2的计时器。接近和离开手动添加


    还请记住,必须从安装计时器的线程发送失效消息。如果从其他线程发送此消息,则与计时器关联的输入源可能不会从其运行循环中删除,这可能会阻止线程正确退出。

    您有多个计时器正在运行。试试这个:

    -(void)startTimer{
        [self.myTimer invalidate]; // kill old timer
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(doSomething) userInfo:nil repeats:YES];
    }
    
    -(void)stopTimer{
        [self.myTimer invalidate];  
        self.myTimer=nil; //set pointer to nil 
    }
    

    我已经尝试了找到的所有可能的解决方案,但无法解决这个问题,最后我在初始化计时器时设置了repeat“false”,如下所示

    self.timer=timer.scheduledTimer(时间间隔:1,目标:self,选择器:#选择器(viewcontroller.methodname),userInfo:nil,repeats:false)

    需要在我的选择器方法中添加上面的行,无论我想重复时间的条件是什么

    例如:-我的要求是重复调用某个方法,直到满足一个条件。因此,我没有添加repeats true,而是将其设置为false,因为repeat true不会使计时器失效

    我在viewdidload方法中添加了以下内容

    self.timer=timer.scheduledTimer(时间间隔:1,目标:self,选择器:#选择器(viewcontroller.method),userInfo:nil,repeats:false)

    在选择器功能中,我添加了以下代码:-

    func method{
       if condition matched{
            // here your timer will be invalidated automatically
       }
       else{
           self.timer = Timer.scheduledTimer(timeInterval: 1, target: self,selector: #selector(viewcontroller.method), userInfo: nil,repeats: false)
       }
    }
    
    希望这能解决你的问题


    快乐编码:)

    显示属性代码。使用弧?不,我不使用弧。关于property@property(非原子,保留)MyClass*fbt;
    tim
    的属性是一个有趣的属性。是否保留?在代码中设置两个断点:一个在创建计时器之前,另一个在停止计时器之前。如果在使
    tim
    无效之前按了第一个两次,则会丢失对计时器的引用(该计时器将继续触发选择器
    repeatTim
    。Is[[fbt tim]invalidate]在与创建计时器的类不同的类中?演示如何实例化fbt。另外,尝试记录fbt以确保它不是nil。我删除了行,现在在dispatch_async(dispatch_get_main_queue(),^{})调用这两个函数,但它在[fbt setTim:nil]上给我EXC_断点;这是什么类型的异常?调用了错误的方法,无异常-尝试调试更多信息。“tim”是您的类中的属性吗?您是否保留了它或其他什么?可能会粘贴整个类实现以便我可以提供帮助。这是我的错误,我检查了所有其他代码,发现我的方法是从一个周期中调用的。感谢大家。