Should popViewController be called from the parent or child ViewController?

Go To StackoverFlow.com

4

I can pop a view controller from a UINavigationController's stack by calling [self.navigationController popViewControllerAnimated:YES]; either from the parent view controller that instantiated it, or from the child view controller itself. Which way is preferable?

2012-04-05 01:29
by Jamie Forrest


3

I suppose the answer is "whatever makes the most sense"; as you said, you can call popViewControllerAnimated: from the parent, child, or even some other class that holds a reference to the navigation controller.

For most apps, the child view will contain a "back" or "done" button or some other action that then leads to it being popped. Generally, I would say this is the preferable behaviour: the child view controller pops itself.

Otherwise, the child would have to call out to the parent (via a delegate or something) to say "pop!" which would then call popViewControllerAnimated:. This feels a little too convoluted for such a simple action.

2012-04-05 02:19
by gregheo
It depends on what kind of "back" button you are providing, the one with left arrow comes from parent view controller - Xiantao Jiao 2012-04-05 02:42
In my case I am calling a delegate method on the parent, because the parent is a tableview "form", and my child is a tableview to browse, search and select from a list of possible values for a field on the parent form. So I could see it making sense both ways, although my gut says to do it in the child because that's where the user action happens (selecting a choice) that should close the view - Jamie Forrest 2012-04-05 09:25
+1 to that. My gut also says if the action is in the child view, then the call to pop should be there too - gregheo 2012-04-05 23:08
Ads