I have a main scene and second scene in my storyboard.
When the app first starts, the main scene viewDidLoad() function is called. Then, I show the second scene. when I go back to first scene viewDidLoad() isn't called.
In the second scene I have implemented the viewDidDisappear() function and it updates a database before the scene disappears.
On the main scene I used viewWillAppear() function to read the same database, but it doesn't work.
Somehow the main scene viewWillAppear is called before the second scene disappear function. It supposed to be other way around isn't it?
Because when you are going back to first page, active story board is disappear and new one is appear.
Can anyone help me to fix this issue?
First, viewDidLoad is only called when the view controller is loaded; when you return to the existing view controller instance, it isn't loaded, the already loaded view controller is simply displayed, so what you see is to be expected.
For your second question, look carefully at the function names;
viewDidDisappear - This function will be called after the view has disappeared as is implied by did
viewWillAppear - This function will be called before the view has appeared as is implied by will
You say "it updates database before the scene disappears.", but by the function name you can see that it will update the database after the view disappears.
You can use the functions viewWillDisappear to update the database as this will be called before the second view controller disappears, and viewDidAppear to update the view after the first view controller has re-appeared.
Alternatively you can use a delegation pattern to allow the second view controller to explicitly tell the first view controller (as its delegate) that the data has changed.
Related
If I have multiple view controllers being presented and dismissed in any order, can I be sure that iOS calls viewWillAppear methods in the right order (i.e. order of appearance)?
I cannot find any specific information about this in the documentation.
I think this is all you need to know about viewWillAppear from the docs:
This method is called before the view controller's view is about to be added to a view hierarchy and before any animations are configured for showing the view. You can override this method to perform custom tasks associated with displaying the view. For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation or style of the view being presented. If you override this method, you must call super at some point in your implementation.
Only thing that comes to mind that might not be absolutely clear is that this callback is called on the presenting view controller when presented view controller is going to be dismissed (so presenting view controller is going to appear again).
Therefore if A is a root, A.viewWillAppear will be called before it will appear on the screen. Then, if A presents B, just before B becomes visible, B.viewWillAppear will be called. And when B is being dismissed, A.viewWillAppear will get called again, since its view will appear again.
viewWillAppear() is called the first time the view is displayed and it is also called when the view is displayed again, so it can be called multiple times during the life of the view controller object.
It’s called when the view is about to appear as a result of the user tapping the back button, closing dialog, or when the view controller’s tab is selected in a tab bar controller, or a variety of other reasons. Make sure to call super.viewWillAppear() at some point in the implementation
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
Hi all I am doing a course in Udemy, and the code calls for placing code in the viewDidLoad function as shown below:
override func viewDidLoad() {
super.viewDidLoad()
placesArray.append(["name":"Taj Mahal", "lat":"27.175607", "lon":"78.042112"])
}
The array append should only run once, however, when I segue to another viewController and come back, it runs the code to append again. So I now have an array with 2 rows, both of which are Taj Mahal.
I thought that the viewDidLoad function only runs code once?
Is there a way around this?
Thanks.
Addendum:
I am using Swift, so I don't see any alloc and init while creating and launching the viewController. And weird as it sounds, the video tutorial has it working in the viewDidLoad and the trainer is using the storyboard to segue from the initial table view controller to a map view on a view controller and just has a back button on the map view that segue's back to the table view controller via the storyboard as well. - Could be because I have the latest version of the Swift language and the trainer was using an earlier version, (cause I noticed some slight differences in coding earlier) but you never know. Either way whenever he touches the back button it does not run the append code anymore.
I am trying to get in contact with the trainer as some of the suggestions here, though they are good don't seem to work.
I will put the solution in here once I get in contact with the trainer.
The viewDidLoad method is called when your view controller's view finishes loading. The view will load when a view controller's view property is nil and something attempts to access it.
UIViewController *myVC = [[UIViewController alloc] init];
UIView *aView = myVC.view; // this loads myVC's view; viewDidLoad is called when it completes loading.
If the view has unloaded (usually due to memory limitations), it will be called when the view property is next accessed.
Manipulation of data sets should generally not be done within view methods. Consider moving this to the init of the view controller (or to a different "datasource" class).
I suppose you are trying to do data initialisation in viewDidLoad. If there is no other operation on placesArray before viewDidLoad, then instead of append, what about setting the placesArray directly:
placesArray = ["name":"Taj Mahal", "lat":"27.175607", "lon":"78.042112"]
Then even if your view is unloaded for some reasons. Taj Mahal will still be added once only.
viewDidLoad is called whenever the view controller's view property is set. When does this happen? It depends on how the view controller is contained:
UINavigationController
- View Controller views are loaded as they are added to the navigation stack and "unloaded" (although the viewDidUnload method is deprecated) as they are removed.
UITabBarController
- View Controller views are loaded as they are added to the tab bar regardless of whether they are on screen or not. They stay loaded as you change from tab to tab.
Depending on your needs and use case, you can create your own view controller container that does what you need. Checkout the Apple docs on the proper way to do this:
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html
What is the difference between viewDidLoad and viewDidAppear? What kind of initialization or custom code goes into those functions?
e.g. presentModalViewController works only when present in viewDidAppear and not on viewDidLoad.
viewDidLoad is called exactly once, when the view controller is first loaded into memory. This is where you want to instantiate any instance variables and build any views that live for the entire lifecycle of this view controller. However, the view is usually not yet visible at this point.
viewDidAppear is called when the view is actually visible, and can be called multiple times during the lifecycle of a View Controller (for instance, when a Modal View Controller is dismissed and the view becomes visible again). This is where you want to perform any layout actions or do any drawing in the UI - for example, presenting a modal view controller. However, anything you do here should be repeatable. It's best not to retain things here, or else you'll get memory leaks if you don't release them when the view disappears.
See: https://developer.apple.com/documentation/uikit/uiviewcontroller
Simply put, you would want to create any controls or arrays in viewDidLoad, where as in viewDidAppear is where you would want to refresh those controls or arrays.
viewDidLoad is called once when the controller is created and viewDidAppear is called each time the view, well, DID appear. So say you have a modal view that you present, when that view is dismissed, viewDidAppear will be called, and viewDidLoad will not be called.
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.