objective-c - 我在一个线程里起了一个runloop,开启了一个timer,但我发现页面返回时不走dealloc,timer无法停止,求大神帮助
淡淡烟草味
淡淡烟草味 2017-05-02 09:35:06
0
1
589
#import "MyViewController.h"

@interface MyViewController ()
{
    NSTimer *_timer;
}
@end

@implementation MyViewController

- (void)dealloc
{
    [_timer invalidate];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeCountdownTime) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

- (void)changeCountdownTime
{
    // do something
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
淡淡烟草味
淡淡烟草味

reply all(1)
Ty80

The recommended way to write is to first write [_timer invalidate]; and then write _timer = nil; in viewWillAppear: before destroying the vc and calling the -dealloc method.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!