Heim > Fragen und Antworten > Hauptteil
#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];
}
PHP中文网2017-05-02 09:37:06
推荐写法是在 viewWillAppear: 中先写 [_timer invalidate]; 然后写 _timer = nil; 之后才会销毁 vc 然后调用 -dealloc 方法。