views:

480

answers:

2

I have a view which contains a UIButton. When this is clicked, it calls a method that loads another NIB. Now, normally, that nib would load a view onto the stack, and everything would be fine. But, I am trying to load a Navigation Controller (so that I can have table views that are multiple levels deep), and all I get it errors.

What is the proper method for loading a Navigation Controller and putting it on the top of the stack?

A: 

Create the navigation controller in the app delegate. Push your mapview onto the stack as the first view. Push your tableview onto the stack as the second. If you started with a view-based app template, you won't have a navigationcontroller instantiated at all. (Been there done that) the easiest way out of this is to use xCode to make a navigation based applcation and then copy the code out of that. If you do already have a navigation controller, then just push the view controllers as above.

Andiih
I do already have a nav controller, but it is a tab bar controller, and it is working and hooked up through my app delegate. I assume this can't be used how I am needing?
Nic Hubbard
The way I create a tab bar controller, each tab has its own navigation controller (so each tab can remember where it is in its own stack - if you do that, it should just work). See http://stackoverflow.com/questions/2802612/how-to-create-iphone-main-menu-screen-with-2-tabbars-subview/2806137#2806137 for details
Andiih
+3  A: 

As the other poster said you should create your Nav controller in your AppDelegate. If you are adding a new UIView to the stack like presentModalViewController you want to create the UIView then add the Nav Controller to it. If you don't want nav controller on that screen but the next just use the navController.hidden property I think it is.

To add the nav controller to the view do this:

NoticesView *noticesScreen = [[[NoticesView alloc] init] autorelease];
noticesScreen.delegate = self;
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:notices_screen] autorelease];
[self presentModalViewController:navController animated:YES];

Hope that helps towards your question. Still trying to find out exactly what your doing

Rudiger
To clarify, here is what I am trying to do. 1. I already have a tab bar controller which works. 2. Click on tab 2, takes me to my mapview. 3. There is a button on the map to change to list view. 4. I want this "list" to slide down like a normal view, and it needs to be a tableview that I can drill down into (which I assumed needed a nav controller). Do I not need a nav controller for this?
Nic Hubbard
Yes. Are you just pushViewController the whole way or you doing any presentModalViewController? If you are just pushing the nav controller is passed on for free, but if you do something like presentModal you have to pass the root Nav controller to it. Just a thought cause I'm still a little lost, in my experience you can only push a UIViewController or a UITableViewController. If you push a UINavigationController class (set in the .h class) its always crashed for me as the Nav controller already exists
Rudiger
Well, what is the difference between pushViewController and presentModalViewController? I don't care how it is done, I just want to be able to have a tableview, that you can drill down into each item. And that table view is presented on top of the stack, called by another view when a "list" button is clicked. Seems so simple, but no one seems to know how to do this!
Nic Hubbard
I would highly recommend learning presentModalViewController. An example of it is in the the address book app when you add a new person and the screen slides up. By the sounds of this though your problem is you are trying to start up a Nav Controller later on in the App. Start it at the root, and if you don't want a nav bar on that screen do navbar.hidden = YES I think it is. Don't start a nav bar half way down a stack. Demo on how to do presentModalViewController is here http://deimos3.apple.com/WebObjects/Core.woa/Browse/itunes.stanford.edu.2024353965.02024353968.2106241593?i=2062791690
Rudiger
Thank you Rudiger, we are on the right track here. I found an example of what I am after. In the Mail app, when you write a new email, and you add a contact in the "TO" field, it slides up a tableview. This is exactly what I am wanting to do. So, I would use presentModalViewController for this? Thanks again!
Nic Hubbard
Yeah you would use presentModalViewController. Although some answers here will show you imo a lazy way to implement presentModalViewController I would highly recommend doing it how they do it in the video demo (watch their demo, don't copy the slides) its hard to explain why but probably further down your App development it will just click that sometimes the delegate way they explain in the video fits perfectly for what you want to do, and would be cumbersome any other way. Hope that makes sense and helps
Rudiger