UINavigationController issue. viewWillDisappear/viewDidDisappear of view controllers not call on application startup - iphone

I am writing an application(for iPhone) that uses the UINavigationController to show different view via pushViewController. I am trying to push multiple UIViewController immediately upon application startup without user interaction. When an user open up the application, they will be looking on a screen with a navigation bar on the top that already have a back button.
The problem I am experiencing is that viewWillDisappear and viewDidDisappear of a ViewController does not always get call if another ViewController is pushed on top of it immediately upon application start up without user interaction. If the ViewController being pushed on top is activate by the user with a button click, then viewWillDisappear and viewDidDisappear of the view being cover always get call.
Note. viewWillAppear and viewDidAppear of the covered view controller always get called.
In the code that is experiencing the problem, I am pushing the new view controller in viewDidAppear of the previous ViewController.
Anyone know what I might be doing wrong or if the behavior I am seeing is expected?

The problem I am experiencing is that viewWillDisappear and viewDidDisappear of a ViewController does not always get call if another ViewController is pushed on top of it immediately upon application start up without user interaction.
Sound pretty sensible to me. After all, the "covered" view never appeared on screen, so it cannot disappear. I would expect this behavior.

Related

why only viewWillAppear called on navigation back

I have doubt need to be clear.. I have stack and a navigation controller.now when the stack loads the viewDidLoad viewWillAppear viewDidAppear will be called. when i click some button then this button push me to the new stack , now new stack gives me the option of the back..now when i click on the back of the navigation controller..why only viewWillAppear will be called ..why not viewDidLoad and not viewDidAppear
Stack is Last In First Out (LIFO), so when you push new view controllers to the stack, previous viewcontroller will not get destroyed( and they remain in memory). When you pop back, there is no need to recreate the Viewcontroller since it is already in memory. So only viewWillAppear gets called.
As to why viewDidAppear doesn't get called in this case, I cant remember where I have read this, but viewDidAppear gets called after your UIViewController's view was added to the application's UIWindow heirarchy. And this process is done before the UIViewController is shown for the first time.
viewDidLoad only called when viewControllers views are loaded into the memory. It will be done when
the first time the view is needed to be shown
sometimes when viewController needed to be reloaded again, because
it is purged from memory for some low memory reason.
In your case, when you pop back, the viewController is already loaded, so no need to call viewDidLoad again.
Full life cycle of ios ui explain here.
http://www.verydemo.com/demo_c134_i4568.html
Note By Abizern from comment: this is true for iOS5 and earlier. iOS6 does not unload views anymore.
First of all, nice question #user2102546. Checkout here the perfect reason for your query.
viewDidLoad only gets called if view controller's views were unloaded, and need to be reloaded.
Normally, if you use a navigation controller, and go back to a previous view with one of the pop methods, viewDidLoad does not get called again, because the view are not unloaded yet.
However, the system can unload the views of any view controller when it is not frontmost in order to free up memory, so viewDidLoad can get called any time a view controller is about to be presented. You need to write your code to respond correctly to the different events.
Enjoy Programming!!
I don't have a complete answer for you but I hope this helps.
viewDidLoad is a callback for modifying a view after the load event has happened. In your case, the view has already loaded. The fact that it is not in view doesn't mean it has been unloaded from memory.
viewDidAppear: While I don't know why this event isn't firing and would be happy if someone else would fill in the gap.

UITabViewController, viewWillAppear does not invoked?

New to iOS, kindly bare if the question is very basic? When I press the tab button multiple times, it is not invoking viewWillAppear function? If I am wrong, then which function gets invoked, each time a tab button is pressed being on the same tabview?
You are correct, viewWillAppear is a little special, it is usually called automatically but in some cases including when you are adding a view controllers view manually (view addSubview:), and also when adding this as a view controller to a UITabViewController it doesn't get messaged.
This however is only for the root view, as you navigate (maybe with a navigation controller) back and forth, that root view's viewWillAppear will get triggered as some point.
In short, if you need to implement something in viewWillAppear in these cases, you should message it yourself when you know it's going to be presented. You can handle this case in your view controller, check out the following article about the matter:
http://www.touchthatfruit.com/viewwillappear-and-viewdidappear-not-being-ca
Good luck.

iOS: return from pushed ViewController via delegate methods?

I've been wrestling with this for almost 2 hours now and no luck.
I have a View Controller. Lets call it First. I press a button and it takes me to Second view controller using a modal style. In Second I add some data which I can save or discard via two navigation bar buttons: Save and Cancel. both do what they do and then they return me to the previous screen. I implemented a protocol and I use the delegate. So far everything works fine.
Today I decided to have a Third View Controller which can be accessed from the Second VC. The problem is I couldn't move to THIRD until I changed the transition from First to Second to PUSH (instead of modal). Now I can go from First to Second and from Second to Third. All good again.
THE PROBLEM : If I press Cancel or Save on the Second VC, it calls the methods, it uses the delegate to go to FIRST and execute some function but when it calls:
[self dismissViewControllerAnimated:YES completion:nil];
nothing happens and Im stuck on the Second view. Basically I was using MODAL with Delegate and protocol methods... I switched to PUSH, and I cant get rid of my Second view and return to First. It seems like the dismissViewController doesnt do anything.
Any help is greatly appreciated. I can fix all this by removing the cancel button and using the BACK which comes by default with PUSH but I just want to know what the problem is with it.. THANK YOU !!
On your 2nd viewContrller call the UINavigationController's method popViewControllerAnimated: or popToRootViewControllerAnimated:. This will pop the controller (the one calling, which is the 2nd) off the nav stack. You can get the navigation controller in a UIViewController via its property navigationController.

iOS Dismis stack of ViewController

I'm developing a iPhone App, in which I'm changing the ViewController using presentModalViewController method upto 2/3 levels (e.g. Home-->Option-->Other-->More).
Now I want to get my app back to first screen or ViewController. If I use dismissModalViewControllerAnimated method, it just dismisses the current view controller only. But I want to clear the stack and start the Home screen again.
Thanks for any help.
Set the delegates properly for each level. So if you press Cancel or Done in More, it will call somehting like [other moreViewDidCancel] inside of which, you will call dismissModalViewController:Animated: and notify its parent view controller (delegate) that is should dismiss( so, [option otherViewDidCancel]) and so on till the top level.

iPhone viewDidAppear stops firing after loading/dismissing a modal view

I'm starting to go a little crazy with this one.
I have an iphone application with a somewhat complex view structure it is a tabbed application with either a view controller or a navigation controller on each tab.
The main controller for one tab uses the viewDidAppear callback to reload any data that has been modified since the last time it was displayed and this behaves fine in most cases. The issue I have run into is that when I navigate in subviews(within the same tab) using the standard navigation controller push/pop mechanism the viewWillAppear and viewDidAppear on the main view always fire when I navigate back to it.
However if I load a modal view controller and then dismiss it, the viewWillAppear continues to fire but the viewDidAppear stops firing.
I am almost certain that this is tied to the need to manually call these callbacks on the modal controller but I cannot find the reference info on how to do this correctly. (I only see this viewDidAppear bug on the hardware, in the simulator it works as I'd expect)
If you need further clarification let me know and thanks for any input.
Yes, this is how it works. You are better off switching to using viewWillAppear.
This has the added advantage of updating the data before the screen is redrawn.
viewDidLoad should be used for one-time setup operations:
Setting titles
Instantiating subviews, content arrays
Anything else related to the infrastructure of the view in question
After that, use viewWillAppear: to do anything related to refreshing data. You shouldn't have to call any of the viewDid/viewWill methods manually – that's handled by the innards of UIView. viewDidLoad won't fire after you dismiss a modal view controller because, more than likely, the view never unloaded. viewDidLoad fires fine when you're navigating the a view controller stack since the new views need memory, requiring other views to unload, then be reloaded when they reappear.