开发者

How to add a Navigation Controller with Interface Builder?

1. Step: Create a new UIViewController:

开发者_运维百科- Xcode -> New File... -> Cocoa Touch Class -> UIViewController

- Name: MyViewController

2. Step: Drag and drop a "Navigation Controller" (UINavigationController) from the Library to MyViewController.xib

How to add a Navigation Controller with Interface Builder?

3.Step: I'm sure, I have to do something to connect the Navigation Controller correctly, isn't it?

4.Step: Try to start the new View Controller as a modal dialog:

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
NSLog(@"navContr: %@", myViewController.navigationController);
[self.navigationController presentModalViewController: myViewController animated:YES]; 

Result: "navContr: nil"

5.Step: You can see the new modal view (MyViewController), but there's no NavigationController and no UINavigationBar.

Thank you very much for your help!


UPDATE 1:

6.Step: I set a new UIViewController (ViewNavi2) as "Root View Controller":

How to add a Navigation Controller with Interface Builder?

7.Step: I define a IBOutlet UINavigationController *navigationController in the class MyViewController and configure the xib: Navigation Controller -> Connections -> Referencing Outlets

But my Navigation Controller is still nil :-(

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
NSLog(@"navContr: %@", myViewController.navigationController);
// -> "navContr: nil"


I also wanted to include the navigation controller in the NIB file, but all that the Apple documentation said was that it is "not optimal" without actually explaining how to do it. I wanted my main tabbed window to call up a modal window containing the typical navigation controls without defining the navigation controller programmatically. I'll share how I did it.

Your code was very similar to mine, as I also defined an IBOutlet UINavigationController *navigationController in MyViewController. From Interface Builder, I simply defined my view in the root view controller and set the File's Owner->view to that view definition.

And similarly to your code, I did the following in my parent window:

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[self presentModalViewController:myViewController animated:YES];

(I am calling presentModelViewController on self, I don't know why you had self.navigationController.) When I tested this, the result was that my view was loaded from the NIB file, but the navigation bar was missing. One more line of code in MyViewController fixed this:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setView:myNavigationController.view];

}

All I am doing here, is setting MyViewController's view to point to myNavigationController.view, which contains everything that was defined in the NIB file, including the navigation controller.


Look at the template in XCode for a "Navigation-based Application". (New Project -> Navigation-based Application) Then open up the MainWindow.xib in Interface Builder. Though it is not a view, think of it as the viewcontroller that you created in step 1. Then look at the "Root View Controller" that is referenced in the inspector and see how it is connected to the "Root View Controller" nib in the Attributes inspector and the "RootViewController" Class in the Identity inspector.

You will want to put the content you want to display at the root of your navigation in your version of a "RootViewController". Typically, the controller that is displayed next is a "DetailViewController". Again, read through the example code that the template provides and you will see how to start on that. Look at the commented code in didSelectRowAtIndexPath in the example template of the RootViewController.m. You will need to create your own view controller to serve as the DetailViewController.

Hopefully this will clear things up for you.

//EDITS based on your Comment//
The Apple docs say that if you want to use IB to make your Navigation controllers you should really put them in your MainWindow.xib. I was tinkering with the fact that you cannot have the NavigationController as a subview of your UIView object. What you can do given where you've started from is to use IB to add a Navigation bar object to your view controller and then control things from there.

From the Apple documentation "Although you could also load standalone or modally presented navigation controllers from your main nib file (or any other nib file), doing so is not optimal. In those scenarios, it is usually easier to create the navigation controller programmatically at the point of use."

//MORE EDITS// In XCode4 the project template is now called a "Master Detail" project. This is because it will be set up as a split view controller in iPad but still a regular navigation project in iPhone. Where I reference "RootViewController" in the answer, you will now find something called "MasterViewController"


The next code is will not give you any valid object.

NSLog(@"navContr: %@", myViewController.navigationController);

The reason is because navigationController property of UIViewController only returns a navigation controller if the view controller is in its stack. This property is nil if a navigation controller cannot be found. Your view controller is not in the stack of the navigation controller. Anyway I even am not sure if you correctly initialize our navigation controller. Read some documentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜