我正在使用UINavigationController。当用户点击后退按钮时,如何在我的计时器上停止(调用invalidate方法)。
答案 0 :(得分:2)
我认为您可以使用viewWillDisappear
方法在按下后退时间接检测并使计时器无效
答案 1 :(得分:0)
//放置此viewDidLoad并确保你有一个模仿后退按钮的back.png
UIImage *buttonImage = [UIImage imageNamed:@"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(backPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
- (void)backPressed:(id)sender {
[yourTimer invalidate];
}
如果计时器在另一个类中运行,请使用NSNotificationCenter在backPressed方法中发布通知
请参阅此处的NSNotification Center示例NSNotification Tutorial