0

I have viewcontroller. I have created its object in appdelegate.m file as I want to use that viewcontroller in poptoviewcontroller method. Now once I have created its object in appdelegate file and when I am pushing that viewcontroller it is calling viewdidload only 1 times. But from second time it is not calling viewdidload. I have some component which i want to load each and every time that controller load. What to do?? Is there any other way to use viewcontroller in popToViewController method without creating its object in appdelegate.m file

I have 4 viewcontroller A,B,C,D. I am navigating from A to B. And I have some component in B which I am loading in viewdidLoad. Now From A to B flow I am getting value from server of component. And displaying in B. Now if user want to change value then he will redirect to C from B to change B component value. And also some times he will redirect to D and from D I am poping to B with B component value. Now If i will write my B code in viewwillappear then When I will return from C to B it will load value which I got at the time of navigation from A to B.

vivek bhoraniya
  • 1,526
  • 2
  • 17
  • 36
  • 1
    it happen because one instance of the object is loaded only into the memory once. for improving your skills of the view-controllers and their lifecycle in iOS, please visit: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html – holex Jan 07 '14 at 09:58

2 Answers2

3

You can use viewWillAppear or viewDidAppear (this last one is called after the first). ViewDidLoad is only called when the ViewController is constructed. Then, since you let it live in the navigation stack it isn't called anymore.

For more information follow this thread on stackoverflow.

EDIT

With your edit the whole question becomes a different one. If you want a delegate to be called every time you will show your ViewController viewWillAppear or viewDidAppear is the answer.

It seems to me that you are over complicating things. From what I understand you are changing B in your AppDelegate? If so I can't see any problem with B having a previous state when you came from C and update your state in viewWillAppear. If you have some kind of pointer in the AppDelegate to B and you change it, then the viewWillAppear shouldn't have any issue.

You can have several solutions for your problem, and it all depends on your specific case but I would suggest that you separate the model better. You could create a singleton that holds B data and in the viewWillAppear you can get that data and display it. Then C and D only perform changes on that singleton.

Community
  • 1
  • 1
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
  • from navigated viewcontroller I can navigate to another viewcontroller to change value of component. So if i use viewwillappear then it will not hold my vlaue. – vivek bhoraniya Jan 07 '14 at 09:47
  • This is true, unless your `UIViewController` gets unloaded because of memory constrains. On that case, `viewDidLoad` will be called again. – Rui Peres Jan 07 '14 at 09:47
  • @vivek if you have a specific problem you need to be more specific in your questions as well. Maybe show us an example? – Tiago Almeida Jan 07 '14 at 09:52
  • @TiagoAlmeida See my explanation. – vivek bhoraniya Jan 07 '14 at 10:02
  • @vivek edited my answer but this is kinda another question you are doing. This has to do with the architecture of your app, not with the viewController life cycle. You are misleading people (and google) with your question. – Tiago Almeida Jan 07 '14 at 10:21
  • means???...I set value of component of B via array which is intialize in A. I have written that code in viewwillappear. So when I will come from C it will agian load same value from that array...and what you said about singleton? can you put more light on it – vivek bhoraniya Jan 07 '14 at 10:34
0

viewDidLoad Method only called on initialisation of class, If you have some task to do again and again whenever view appears on screen than use viewWillAppear or viewDidAppear Method.

Hemant Chittora
  • 3,152
  • 3
  • 19
  • 25