I understand that you can use ViewDidLoad to run some code when a view is loaded. However that only happens once. How can I run a method every single time that view is shown. So for example: let says you are currently in ViewController A and you press a UIButton to go to ViewController B. Then you press a button to go back to ViewController A, how would you then re-run the ViewDidLoad code??
I hope my question makes sense. In essence I want to re-run a small method every single time the user is on a particular ViewController.
Thanks for your time, Dan.
viewWillAppear:
Notifies the view controller that its view is about to be added to a
view hierarchy.
or
viewDidAppear:
Notifies the view controller that its view was added to a view
hierarchy.
This answer didn’t help. I found myself in the same situation and the solution is simple.
Create a method with codes you want to be executed every time your view controllerA shows up.
Place your method under viewDidAppear()
Make sure that the modal type of your viewcontrollerB is set to fullscreen (fullscreen removes viewcontrollerA from the stack)
Under viewcontrollerB viewDidLoad()
Set its background other color than clear. White works for me
Dismiss from your viewControllerB and “voila!”
Create an AbstractController that every view controller inherits from that controller, and you override the viewDidLoad method and make it do whatever you want at every view opened.
Related
here is my scenario case.
Initially for going to this VC without loading is hidden.when I click to first view controllers button it goes to second view controller.When I click button from secondVC it come back to first one and for going to this VC without loading button is now visible.Now when I click for going to this VC without loading I want to show my second view controller without reload because my previous loaded data for second view controller is needed.So how can I do that?
the actual scenario of my app look like this.My first VC
and the second one.
It's a picture of sound cloud but the case is same.
First possible solution,
Add SecondViewController as child view controller of FirstViewController using container view in Storyboard.
Every time you want to remove SecondViewController just hide/remove it with custom animation block.
Keep the reference of SecondViewController in FirstViewController
Second possible solution,
Create shared data object.
Then you can use that shared data object in any view controller, regardless of saving the state of any view controller.
I would create an object where i put the data und pass this from ViewController to ViewController by properties. Maybe this is to simple but it should work.
Please help me with this. I have created a simple project with two views as shown. I have attached the images for my storyboard and swift files. So, I read that viewdidload will be executed only once while loading the view into the memory. But, when I make a transition from secondview to the firstview the viewdidload is executing again and so is the print statement in the viewdidload method.
Someone please explain me this.
viewDidLoad is not called once for the Application. It is get called once for that viewController when the view holds memory and loaded.
So as many number of of time you push to the viewController, that many times it will call the viewDidLoad
viewDidLoad() — Called when the view controller’s content view (the top
of its view hierarchy) is created and loaded
viewWillAppear() — Intended for any operations that you want always to
occur before the view becomes visible.
For more info about this look at the link : https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson4.html
So if the view is already in memory (Like your case), then no need to push again, only need to pop back by this code
self.navigationController?.popViewControllerAnimated(true)
You should not make transition from secondViewController to firstViewController for back. Pop the second view controller by this code to back:
self.navigationController?.popViewControllerAnimated(true)
When you make a transition it makes a new instance from your firstViewController but when you pop the second view controller it dismiss your secondViewController and shows your last viewed viewController again.
Or
in the case that you are not using navigationController you should use below code to dismiss your secondViewController
self.dismissViewControllerAnimated(true, completion: {});
The main point is that you should not use new transition for back.
The simplest way:
1.First embed your ViewController in NavigationController
2.Call to this (instead of create segue for backing)
navigationController?.popToRootViewController(animated: true)
viewDidLoad will be called only once
I am using navigation controller. I have pushed two viewcontroller to navigation stack. When I am coming back to viewcontroller1 from viewcontroller2 using back button of navigation bar then viewdidload method of viewcontroller1 is called again.But as much as I know viewdidload is called only once at loading time. So why is this happening? Please tell me.
Thanks!!
-(void)viewDidLoad called only when view controller is loaded
but if you want to call any method then you can write code in
-(void)viewWillAppear
this method called every time when your view is appear.
About viewDidLoad
viewDidLoad: is called every time your view controller's view is loaded, not just the first time. The controller's view can be loaded and unloaded multiple times during the lifespan of the controller and viewDidLoad will be called every time. It may be unloaded whenever it's not on screen, usually if memory is low.
Best practices
Remember not to do view controller initialisation in viewDidLoad. This is a common mistake. For stuff that should only happen once when the view controller is loaded, do it in one of the controller's init methods.
If you're popping/dismissing back to it, viewDidLoad is not generally called, but viewDidAppear will.
The exception to this is in iOS versions prior to 6.0, if you received a memory warning, your view could be unloaded, and it will be reloaded when you pop back.
As you are pushing the viewcontrollers, AFAIK they create a new instance of the view controller they are presenting. When you get back to viewController1 it's viewDidLoad will not be called but the viewController2 viewDidLoad will be called every time you move from viewController1 to viewController2. When you perform pop from viewController2 it is deallocated there itself
I have an iPhone application which consists of two View Controllers; a main one, and one with the help screen. Each one has a button that performs the segue from one to the other.
The problem I have is, when I segue back from the help screen to the main screen, the main view controller's viewDidLoad method gets called, so all of the initialization I did when the app was first started is repeated. Is there another method in the view controller that gets called just once, where I can do the initialization?
My first thought was, "Have a boolean variable that is initially set to false, then have viewDidLoad test it, and if it is false, do the initialization, then set it to true" - but how do I initialize it to false in the first place?
My guess is that you're doing a "Push" segue (which is the most standard kind of segue one can do in an iOS app), and if you are using a "Push" segue that means you have a navigation controller in your app.
The best thing to do here is not to push another "Main" view controller onto the stack of other view controllers (which is why you are seeing "viewDidLoad" called each time you push the main view), but instead when you click a "go to main" button in your help screen, pop the help screen off and return to the previous one. The call that would do this is UINavigationController's popViewControllerAnimated method.
Doing that means "viewDidLoad" on that view controller only gets called once, as the main view gets loaded once.
on the .m class file create a bool on #implementaition:
#implementation yourClass{
bool initialize = 0;
}
and then test it on view did load:
-(void)viewDidLoad{
if(initialize == 0){
//do everything you need to do
initialize = 1;
}
}
I think it will work...
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.