Reputation: 95
I never thought this would be complicated, but apparently it is.
I have a tabbed view controller. One of the ViewControllers has a timer.
Once another view controller is selected using the tab controller, or any other view is selected (using a table view row selection), I would like the timer to stop.
I'm not sure why but I can't figure out how to do this. I assumed I could put the timer.invalidate() in the viewDidAppear() in the ViewControllers associated with the tab selections, but it isn't working.
Any suggestions?
Upvotes: 3
Views: 895
Reputation: 2781
Views have a number of notifications they can receive, for which you can write a handler.
One of these, as mentioned in my comment, is viewWillDisappear.
They are:
viewWillAppear:
viewDidAppear:
viewWillDisappear:
viewDidDisappear:
Together they allow you react to the coming and going of views.
The documentation on UIViewController and UIView is something worth the time to read in depth, more than any other, since they are the basis of all display and interaction in Cocoa.
Upvotes: 2
Reputation: 10951
Try to use tabBarController:didSelectViewController:
Tells the delegate that the user selected an item in the tab bar.
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
if (viewController == targetViewController) {
viewController.timer.invalidate()
}
}
Upvotes: 1