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

Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
Kirti
  • 573
  • 2
  • 8
  • 18
  • chck once are u used for navigation in **Push** or **Pop** – Anbu.Karthik Nov 18 '15 at 11:08
  • 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 add – Anbu.Karthik Nov 19 '15 at 10:59
  • @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. – Kirti Nov 19 '15 at 11:17

4 Answers4

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.

Ashish Thakkar
  • 944
  • 8
  • 27
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.

Community
  • 1
  • 1
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • 1
    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!! – Kirti Nov 19 '15 at 12:09
  • Other than memory being low...is there any other time when the viewController can get unloaded?! – mfaani Apr 16 '17 at 23:05
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.

Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
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

harsha yarabarla
  • 506
  • 4
  • 11
  • 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!! – Kirti Nov 18 '15 at 12:19
  • If you have low memory then viewController1 will also gets deallocated, and gets allocated again when navigated back to viewController1 – harsha yarabarla Nov 18 '15 at 12:22