I hope this is a simple question. If I have a UINavigationController and I push a new view controller onto the stack with an animated transition, how can I detect when the animation has finished and the new view controller is on screen?
I have a few scenarios where I need to push a new controller that then has to do a long-running operation. I’d like to push the new view first so there’s something on screen before I start blocking the main thread for a long time. If I do the push immediately followed by my long-running task the view won’t show up until after both are done of course and the main thread is able to process events again.
So, what I’d like to do be able to detect in the new controller once the animation is done and the view is on screen, and then start the task.
+1 to @DHamrick’s recommendation for not blocking the main thread at all.
To answer the original question, you can detect viewController changes in two places:
The viewController you just pushed will receive viewWillAppear: and viewDidAppear: messages. If you want to know when a specific viewController appears, implement these methods.
The
navigationController:didShowViewController:animated:method mentioned by @Mike Z is sent to the navigationController’s delegate. You will need to assign an object to be that delegate in order to receive this message. You will then know every time a viewController appears.