How to stop NSTimer

后端 未结 7 591
我寻月下人不归
我寻月下人不归 2020-12-03 06:33

Hey so i am just making an example application and i am having a little trouble, So i have a table view and then i have a few rows and when a user clicks a row it takes them

相关标签:
7条回答
  • 2020-12-03 07:12

    You Could Use Two Different Things

        [timername invalidate];
    

    Or You Could Use

        timername = nil;
    
    0 讨论(0)
  • 2020-12-03 07:18

    like this:

    [yourtimername invalidate];
    

    But be careful, you cant stop the timer if it's already stopped because your app will crash.

    0 讨论(0)
  • 2020-12-03 07:20

    Use the below code. It will work But keep in mind that it must only be called if our timer is in running mode else the application will get crashed.

    - (void)viewWillDisappear:(BOOL)animated {
        //BEFORE DOING SO CHECK THAT TIMER MUST NOT BE ALREADY INVALIDATED
        //Always nil your timer after invalidating so that 
        //it does not cause crash due to duplicate invalidate
           if(timer)
           {
             [timer invalidate];
             timer = nil;
           }
        [super viewWillDisappear:animated];
    }
    
    0 讨论(0)
  • 2020-12-03 07:24

    Trick is to use a singleton class, google myGlobals class which is mostly used as a singleton Although if u do wish to use NSTimer, then

    lets say the view 1 is which u go into and view 2 is the one which u go back 2.

    So in case of view 2, write the NSTimer to invalidate the timer in the viewDidLoad(Assuming you have two separate classes for both views).Problem will occur because when u go to view 1, the view 2 Timers will be dealloc. So, make a Singleton class and save the NSTimer pointer like this

    //to be done in viewDidLoad function
    theGlobals *globals = [theGlobals alloc] init];// for the singleton class to initiate,
    [globals.myTimer invalidate];
    
    
    //to be done in viewDidUnload
    theGlobals *globals = [theGlobals alloc] init];// for the singleton class to initiate,
    globals.myTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0
                                                     target: self
                                                   selector:@selector(onTick)
                                                   userInfo: nil repeats:YES];
    

    oh , and here is the myGlobals class code that u will need

    //theglobals.h file
    
    #import <Foundation/Foundation.h>
    
    
    @interface theGlobals : NSObject 
    {
        NSTimer *myTimer;
    }
    
    @property (nonatomic, copy) NSString *changeyes;
    
    
    
    +(theGlobals*)sharedInstance;
    @end
    
    //implementaton .m file
    
    #import "theGlobals.h"
    @implementation theGlobals
    
    @synthesize myTimer;
    
    
    
    static theGlobals *sharedInstance;
    
    - (id) init
    {
    return self;
    }
    
    +(theGlobals*)sharedInstance
    {
    if (!sharedInstance)
    {
        sharedInstance = [[theGlobals alloc] init];
    }
    return sharedInstance;
    }
    @end
    
    0 讨论(0)
  • 2020-12-03 07:25

    Just invalidate the timer

    [timer invalidate];

    0 讨论(0)
  • 2020-12-03 07:27

    As iCoder says it has to be invalidated in viewWillDisappear.

    I added the following just to make sure. it works for me. Hope it helps (and adds to icoders answer do not intent to copy)

    - (void) startTimer
    {
    [self stopTimer];
    timer = [NSTimer scheduledTimerWithTimeInterval: 5.0
                                                         target: self
                                                       selector:@selector(onTick)
                                                       userInfo: nil repeats:YES];
    
    }
    
    - (void) stopTimer
    {
        if (timer) {
            [timer invalidate];
            timer = nil;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题