用户点击后退按钮时停止计时器运行(UINavigationController)

时间:2012-12-30 03:11:07

标签: ios

我正在使用UINavigationController。当用户点击后退按钮时,如何在我的计时器上停止(调用invalidate方法)。

2 个答案:

答案 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