Does viewDidload method call again on going back to a screen in navigation controller?
Asked Answered
I

4

6

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!!

Intertwist answered 18/11, 2015 at 11:7 Comment(3)
chck once are u used for navigation in Push or PopSkyward
ya correct ,if you are used PUSH, the new view controlller is alocated that the reason the viewDidload method called again and again , in here you need to use POP the viewDidload is not call, in this place the viewWillappear is called. got it my bro, if you need the code I addSkyward
@Anbu.Karthik...how can i use POP if i go to viewcontroller1 to viewcontroller2...in navigation controller we push controller on stack, we use POP if we come back.Right?? And i am coming back to viewcontroller1 by using navigation bar back button.Intertwist
L
4

-(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.

Lengel answered 18/11, 2015 at 11:11 Comment(0)
B
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.

Babbie answered 18/11, 2015 at 11:11 Comment(2)
I think this might be right answer because now viewDidload is not called when i go back..may be this time memory is not low. But still i am not sure!!Intertwist
Other than memory being low...is there any other time when the viewController can get unloaded?!Aged
M
1

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.

Maes answered 18/11, 2015 at 11:53 Comment(0)
L
0

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

Liquid answered 18/11, 2015 at 11:24 Comment(2)
i am using "self.navigationController?.pushViewController(viewcontroller2, animated: true)" for navigating to controller2 and using back button of navigation bar for going back to controller1 and viewdidload is called whwn i go back!!Intertwist
If you have low memory then viewController1 will also gets deallocated, and gets allocated again when navigated back to viewController1Liquid

© 2022 - 2024 — McMap. All rights reserved.