views:

96

answers:

1

I'm trying to implement a navigation controller with some hierarchical views. I want to use a regular UIViewController to present choices for drilling down, I don't want to use the navigation bar - I want to have my own, custom buttons for returning back up a level.

I see examples like:

[[self navigationController] pushViewController:nextViewController animated:YES];

and my questions are these: Is navigationController a property of all UIViewControllers? Can I refer to self.navigationController regardless of the view that's on the stack? If I'm at an arbitrary view, can I have a button action that contains something like [self.navigationController popToRootViewController animated:YES];

Each view I present will need a button to return to the previous view, or to the root view, depending on the situation. I want to create that button in each view controller and control which view in the stack it returns to. Am I on the right track?

+2  A: 

Is navigationController a property of all UIViewControllers?

Yes.

Can I refer to self.navigationController regardless of the view that's on the stack?

Every UIViewController on the UINavigationController's stack will return the UINavigationController object when calling navigationController on it.

If I'm at an arbitrary view, can I have a button action that contains something like [self.navigationController popToRootViewControllerAnimated:YES];

Yes. popToRootViewControllerAnimated: will take the user to the root UIViewController for the UINavigationController, and you can use [self.navigationController popViewControllerAnimated:YES]; to just pop off the top UIViewController. This last one does the same as tapping the Back UIBarButtonItem.

Am I on the right track?

Yes :)

Douwe Maan
Thanks! I'm still on the steep part of the learning curve, but each step is pretty rewarding. Looking at this navigation controller stuff a few months ago was utter gibberish to me and now it's looking (fairly) clear. Without Stack Overflow I'd have given up long ago. Nice, clear answer!
Steve
Believe it or not, but only 4 months ago, I didn't know the first thing about C, Objective-C, Cocoa Touch or iPhone app development in general :) If you just really set yourself on learning it (read a few books, build an app yourself from scratch), before you know it you are on Stack Overflow answering other people's questions ;)
Douwe Maan
I'm working through Beginning iPhone 3 Development now and getting the games book next.This works great by the way. I can push a new controller onto the stack and pop it back off, and get rid of the nav bar. My two new problems are: When I push the new controller on, I immediately bring a modal view up to make a choice - how do I get the underlying view to wait until the modal appears before becoming visible (you can see it sliding in to the left as the modal is sliding up). And then, how do I get that choice info from the modal back to the underlying controller?
Steve
I read Beginning iPhone 3 Development and More iPhone 3 Development, I really recommend reading the latter if you want to dig a little deeper in stuff like Core Data and more advanced APIs. I'm getting the Beginning iPhone Games Development book too, asap.
Douwe Maan
I think it's smartest to first present the modal VC, and after that's done, push the other VC on the navigation stack, you can't really stop the VC from being slided in...To get the choice info from the modally presented VC to the presenting VC, you can use delegation. Set the `delegate` on the modal VC to the presenting VC, so the modal VC can report back when a choice has been made.
Douwe Maan
I think I understand the concept of making the presenting VC the delegate of the modal VC, but the implementation isn't clear to me. Doesn't the modal VC know who its parent is, and couldn't it access its parent's properties? That sounds a little hokey though...
Steve
You should always try to make VCs as independent of others as possible, that's where the delegation principle comes in. All the modal VC needs to know about the VC that's its `delegate`, is that it implements a couple of predefined methods (using a `@protocol`), that it can call to report specific information to it. In this case the `parentViewController` will be the same as the `delegate`, but implementing delegation is never a bad thing.
Douwe Maan
You don't know of a good, simple example that shows this do you? I read all this stuff about it and look at some examples but I still don't see how that gets the choice I make in the modal VC back to the parent VC.
Steve
Well, this is used a couple of time in the `More iPhone 3 Development` book, but I don't know of any online sources on how to do this...
Douwe Maan
I have that book but haven't started it yet as I'm not quite done with the one before. Skimming through it I don't see a good example that's not buried in a lot of other stuff I don't get. Could you point out a particular example? Sorry to drag this out so long.
Steve
On page 296 and 297 (eBook edition) there is talk about 'creating' your own delegate, but it's not really stand-alone, you kinda need the rest of the chapter to understand it... You could give it a try and see if you get it though.What I did is first read Beginning ... and More ..., and only after that I started working on my own application, maybe you could do the same?
Douwe Maan
Thanks for the advice and I will continue reading the books, but I won't be able to relax until I at least setting this issue. I tried making a new project to just present/dismiss a modal and update a textfield after watching lecture 11 from the Stanford course. I just about have it working with one compiler warning that crashes me. I'm going to post a new question with my code... Thanks again for the support!
Steve
Okay, I'll try to answer your new question ;)
Douwe Maan