I was successfully using SideMenu 2.3.4 in my app, then upgraded to v3.1.4. I made the few mods required to adjust to API changes, then ran my app - it crashed after the following UI actions:
Open the menu by tapping on the nav bar button
Tap on item that loads another view (the menu closes, the new view appears)
Tap on the nav bar button to open the menu again
The menu didn't open in response to that last tap before the app crashed. Under the debugger, I saw that after 2, viewDidLoad then viewWillAppear were called for that new view. After 3, viewWillDisappear, viewDidDisappear and viewWillAppear of that same view were called, all without any change in the display.
The crash happened in viewWillAppear because my code counts on viewDidLoad being the first thing that is called after viewDidDisappear. AFAIK, it is legitimate to count on that, since viewDidDisappear is supposed to mean that the view was closed and thus has to be reloaded and viewDidLoad called before any other life-cycle method can be called, i.e., viewWillAppear should not have been called before viewDidLoad.
Am I misunderstanding the iOS view life-cycle? Is this a bug in SideMenu 3.1.4?
Thanks,
Mark
A closer reading of the docs and articles on the net show that I did misunderstand the life-cycle methods.
There is no opposite of viewDidLoad (e.g., viewDidUnload), but in viewDidDisappear one can do this:
if (isBeingDismissed || isMovingFromParentViewController) {
// Do something - viewWillAppear will NOT be called before viewDidLoad.
}
That eliminates the crash I was seeing.
Mark
Related
Whenever we present or push from SourceViewController to DestinationViewController, ViewDidLoad() method called every time in DestinationViewController in xCode 9.4 and iOS 11 or later. And when we pop or dismiss from DestinationViewController to SourceViewController, viewDidLoad() method not called.
ViewDidload method only called only when the view loads first time. When you present or push it loads the view so, it calls viewdidload. But when you pop it release the view.
Here is the lifecycle of UIViewController.
ViewDidLoad - Called when you create the class and load from xib. (Either present or push) This method called for initial setup and only one time called.
ViewWillAppear - Called right before your view appears, this will be called every time your view is about to appear on the screen.
ViewDidAppear - Called after the view appears - great place to start
an animations or the loading of external data from an API. This will be also called every time after ViewWillAppear when view appeared on the screen.
ViewWillDisappear/DidDisappear - Same idea as ViewWillAppear/ViewDidAppear only when view is about to dismiss or pop.
ViewDidUnload/ViewDidDispose - In Swift or Objective C, this is where you do
your clean-up and release of stuff, but this is handled automatically
so not much you really need to do here.
Read Apple documentation for details.
I'm learning to code in Objective-C by developing a simple app.
I'm using a UISplitViewController which has UIBarButtonItem.
When you tap an item on the left UINavigationController, shows up on the right one.
This right one has a button inside which takes you to a web site, controlled by a browser controller; when tapped, up left on the UIBar appears a button to go back/to the previous view.
The problem is that when I tap this button, the app crashes and Xcode tells me the problem is the method viewWillDisappear in the browser controller.
I've checked it up and down dozen times and can't find the error.
Change
[self viewWillDisappear : animated];
to
[super viewWillDisappear : animated];
Your way creates an infinite recursion - method calling itself - visible also in Thread 1 call stack.
This question already has answers here:
iOS 7 - Difference between viewDidLoad and viewDidAppear
(7 answers)
Closed 6 years ago.
I always thought that viewDidAppear was called whenever your view appeared on the screen, but I've been told that, for example, when you background an app (by pressing home button) and then bring it back up, viewDidAppear is not called (going to background "doesn't remove the current view from the view hierarchy"). So, what does it actually mean for a view to "appear"? Also, what does it mean for a view to "load", ie. when does it actually happen (for example, when the app is opened by touching the app icon, etc.)
Is Heirarchy Of this:- Alwys Whenevr Your View Controller Runs It Go Like This
1st ViewDidLoad
2nd ViewAppear
3rd ViewDidAppear
4th ViewWillDisAppear
5th ViewDidDisAppear
Last 6th ViewDidUnload
You Can Understand This by This Simple Life Example :-
Suppose You Are In Cafe And
1st==> You Ordered Coffee Then Service Here Your Call Then They Fill Your Coffee On The Cup (Note Loading Or Filling All Contain Like Coffee on Cup Is Called ViewDidLoad )
2nd==>> And When Service Put Coffee On Your Table (Note Is Called ViewWillAppear Where Your Coffee just Like Your ViewController View )
3rd==>> And When You See Your Coffe (Note Is Called ViewDidAppear Where Your View Can See On Your Screen Just Like When Your See Your Coffee )
4th==>> After That When You Finished Your Macachino Coffee And is Empty (Note is Called ViewWillDisAppear Where Unloading or Proccess Of Empty is Stands For ViewWillDisAppear )
5th & 6th==>> And After That When Service Came And It's Pick Up Your Coffee Cup And Take Back From You When Is Did Disappear From Your Eyes (Note is Called ViewDidDisAppear When View or Your Screen Is Go Blank Just Like Your Cup ) And Finally All Proccesss Done Here....
And If You Again ordered Diff. Coffee Aur Same Coffee That All Step Called Again Same Like That, You Have Multiple ViewController And They Call Again Again A--B--A--B
Thx For Listing This Story Happy Coding
viewDidLoad is called when all outlets are initialized from a Storyboard.
viewDidAppear calls when a View Controller is added to another view controller hierarchy. Usually after all animations is finished, but not necessary.
If you implement custom controller which will contain some child view controllers, you will call didMoveToParentViewController of the child controllers when they are added to the parent. So, whenever you call this method viewDidAppear of child VC's will be called automatically.
viewDidLoad is called after your view is loaded. It is called only once when view is initialized and pushed or presented.
viewDidAppear is called once you see the loaded view on screen. It is called after view appeared.
ViewDidAppear is called everytime when you see the view after it is loaded. if you push and then pop any other viewController on that view then again viewDidAppear gets called.
Lifecycle of view Controller:
ViewDidLoad
ViewWillAppear
ViewDidAppear
I'm looking into the viewDidLoad and viewDidAppear methods to better understand what they both do and I came across an article which uses the example of a banking application to explain how these methods work:
Consider a banking application that shows your current balance. A user
can tap on a button, which presents a list of nearby ATMs in a modal
view controller. In order to get the list of nearby ATMs, the
application must make a core location and web service request.
In this situation, a programmer could get away with requesting the
list of nearby ATMs from the server in viewDidLoad. The view
controller is only presented once, both viewDidLoad and
viewWillAppear: will be called back to back and only once for that
particular view controller instance. The net effect of the code in
either of these two methods will be the same.
But this is a bad idea. Consider what would happen if you wanted to
move the ATM view controller into a tab bar controller. Now, the ATM
view controller – with its ATM fetching code in viewDidLoad only
fetches the list of ATMs once. So you are in Atlanta on Tuesday, open
up your application to look for an ATM, then check your balance. Then
you travel to New York on Wednesday, re-open the banking application,
and you only see ATMs in Atlanta. The view was already loaded, no need
to call viewDidLoad and now you’re looking at stale data.
Sadly, I still don't fully understand how/why both viewDidLoad and viewWillAppear will be called 'back to back', or what adding the ATM view controller to a tab bar controller means in terms of these methods.
viewDidLoad method will call only once a life time of viewController and that is when viewController object will first load in memory.
where as viewWillAppear method will call every time when a view will appear to screen or you can say will be topViewController...
Explanation:
Consider you have tab based app with two tabs. Tab1 associated with viewController1 and tab2 is associated with viewController2. Now when you will run your app and you will see tab one is selected and viewController1 is on view and you want to change to tab2, when you will tap on tab2 then tabVieController2's object will create and load to memory first time hence its viewDidLoad method will call, and soon after that it will appear to window and viewWillAppear will also get call.
Now if you you try changing tabs by click on them only viewWillAppear methods will get called for both, as they are in memory already.
It simple, viewDidLoad get called when the view is load in, either via NIB, storyboard or with the loadView method. The viewWillAppear: is called when the view is presented.
When a view is added to a tab bar it only gets load once, thus the viewDidLoad will only be called once. But if the user switch to an other tab and back to the same view the viewDidLoad will not be called. This is because the view is already loaded.
However the viewWillAppear: is called in both cases just before the view is shown. Thus this will be called when the user first opens the tab and when it switches back to that tab.
I think they are referring to the fact that the view is loaded every time the modal view controller appears (thus the data is constantly refreshed) but only once when it is part of tab bar (only loaded on app launch). Kind of a whacky example to explain the methods though.
You might want to read up on the view controller lifecycle to know when to implement what in which method:
Responding to Display-Related Notifications
View Loading and Unloading
I got two viewControllers using a navigation bar. The first viewController displays some data I change on the second viewController.
So if I load the second viewController, a back button appears in the NavBar and I can change my values (and they are stored, I used the debugger). My problem is, after hitting the backButton to come to my firstView Controller, it does not call it's viewDidLoad method. It's clear, that there are no updated values at all, when this function is not called.
At the first start, the viewDidLoad method is called and does what I want it to do. After going back and forth between the viewControllers the method is not called again.
Any solutions?
Thanks!
EDIT:
SOLVED
I did not want to delete my question, maybe someone needs this too:
This method is called every time the view appears, it is probably not defined by default:
-(void)viewDidAppear:(BOOL)animated
{
NSLog(#"View appeared.");
}
But the update code (like [[self view] setNeedsDisplay];) in viewWillAppear.
To make it clear: viewDidLoad is called when your view is loaded. This happens at the first time the view is going to be displayed. When you then navigate to the next view the first view can (depending on your code) still be loaded. Therefore when you navigate back to that view viewDidLoad won't be called because the view is still there.
But every time the view is going to be shown (for example when you navigate back to this view) the method viewWillAppear and viewDidAppear will be called.
Hope that helps.
ViewDidLoad method will get called when there is view controller allocation-initialization happens. In the case of moving back in navigation controller, it is not creating any new view controllers, it is reusing previous references from navigation stack.
Hence viewDidLoad will not get called. As your view controller is already in memory stack. So it will just make the view to reappear on windows and it will call viewWillAppear respectively.