views:

536

answers:

1

I'm starting an app where I'd like to have multiple view controllers. Some of the views will be displayed inside of a navigation controller. I can create a navigation controller and then add another instantiated view controller to it. But what I'd like to do, is just instantiate a view controller that has its own view and is the root view controller of a navigation view controller. So when I instantiate the view controller, I'd like for it create a navigation controller and push "self" on to it. When I do it my simulator crashes and the details don't really give a reason. The console does not display anything. Any ideas. My reason for this is to separate out logic without have a view controller that simply creates a navigation controller and then pushes another view controller on it as the root view controller.

+2  A: 

I'm not entirely sure if I understand your question correctly. Why would it be preferable if the view controller pushed itself to the navigation controller? I mean, you have to instantiate your view controller at some point in code (either app delegate or another view controller) anyway. Why can't you just create the navigation controller there, instantiate your VC and push it onte the nav controller? As far as I can see, this doesn't involve creating any additional view controllers.

Anyways, having a view controller decide by itself where it is used (ie. pushed onto), is not very good practice. This way you loose the flexibility of using it in other contexts. Always try to couple your components as loosely as possible.

Daniel Rinser
I see your point. I am just starting my application and my idea was that for another view controller to use this view controller, it could just instantiate it and the logic of whether or not it should be in a navigation controller would be transparent to the parent. I'm trying hash out how I will manage my multiple view controllers and how they will interact. Haven't been able to find much documentation covering this besides the basic when to use a navigation controller, tab controller..., and the like.
Brian
Well, another point that I forgot to mention: Your navigation controller must have an owning parent of some kind. Either it is added to a tab bar controller, instantiated as your main view controller in your app delegate or pushed as a modal view controller onto some other view controller. How is your sub-view controller supposed to do that? Who will own the navigation controller (wrt. memory management)?
Daniel Rinser
"my idea was that for another view controller to use this view controller, it could just instantiate it and the logic of whether or not it should be in a navigation controller would be transparent to the parent" -> I guess this won't work, because in that case the parent would have to present the navigation controller rather than your sub-view controller.
Daniel Rinser
Thanks for your help. What I ended up with was my appDelegate instantiates my root view controller. The root view controller then, can instantiate other view controllers based on the state of the app. One of those view controllers being a view controller that manages preferences with a navigation controller. I think this is a better way to go since I can encapsulate logic specific to preferences in this view controller and the individual preference view controllers can handle the logic for their preference page. I'm currently trying to show a login pref page if the user hasn't entered a login
Brian