suchen

Heim  >  Fragen und Antworten  >  Hauptteil

objective-c - 我在一个线程里起了一个runloop,开启了一个timer,但我发现页面返回时不走dealloc,timer无法停止,求大神帮助

#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];
}
淡淡烟草味淡淡烟草味2757 Tage vor624

Antworte allen(1)Ich werde antworten

  • PHP中文网

    PHP中文网2017-05-02 09:37:06

    推荐写法是在 viewWillAppear: 中先写 [_timer invalidate]; 然后写 _timer = nil; 之后才会销毁 vc 然后调用 -dealloc 方法。

    Antwort
    0
  • StornierenAntwort