问题
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!!
回答1:
-(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.
回答2:
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.
回答3:
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.
回答4:
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
来源:https://stackoverflow.com/questions/33778426/does-viewdidload-method-call-again-on-going-back-to-a-screen-in-navigation-contr