如何使用Interface Builder添加导航控制器? [英] How to add a Navigation Controller with Interface Builder?

查看:126
本文介绍了如何使用Interface Builder添加导航控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<强> 1。步骤:创建一个新的UIViewController:

- Xcode - >新文件... - > Cocoa Touch类 - > UIViewController

- 名称:MyViewController

1. Step: Create a new UIViewController:
- Xcode -> New File... -> Cocoa Touch Class -> UIViewController
- Name: MyViewController

2。步骤:将导航控制器(UINavigationController)从库中拖放到MyViewController.xib

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

3.Step:我敢肯定,我有要做些什么来正确连接导航控制器,不是吗?

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

4.Step:尝试将新的View Controller作为一个启动模态对话框:

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]; 

结果:navContr:nil

Result: "navContr: nil"

5.Step:你可以看到新的模态视图(MyViewController),但没有NavigationController,也没有UINavigationBar。

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!

更新1:

6.Step:我将新的UIViewController(ViewNavi2)设置为Root View Controller:

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

7.Step:我定义了一个 IBOutlet UINavigationController *在MyViewController类中的navigationController 并配置xib:Navigation Controller - > Connections - > Referencing Outlets

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"


推荐答案

我也希望将导航控制器包含在NIB文件中,但Apple文档中所说的只是没有最佳而没有实施lly解释如何做到这一点。我希望我的主选项卡窗口调用包含典型导航控件的模态窗口,而无需以编程方式定义导航控制器。我将分享我是如何做到的。

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.

您的代码与我的代码非常相似,因为我还定义了一个 IBOutlet UINavigationController * navigationController 。在Interface Builder中,我只是在根视图控制器中定义了我的视图,并将File的Owner->视图设置为该视图定义。

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];

(我自己调用presentModelViewController,我不知道为什么你有 self.navigationController 。)当我测试它时,结果是我的视图是从NIB文件加载的,但导航栏丢失了。 MyViewController中的另一行代码解决了这个问题:

(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];

}

我在这里做的就是将MyViewController的视图设置为点到myNavigationController.view,它包含在NIB文件中定义的所有内容,包括导航控制器。

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.

这篇关于如何使用Interface Builder添加导航控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆