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.
非常感谢您的帮助!
更新 1:
6.Step:我将新的 UIViewController (ViewNavi2) 设置为根视图控制器":
6.Step: I set a new UIViewController (ViewNavi2) as "Root View Controller":
7.Step: 我在 MyViewController 类中定义了一个 IBOutlet UINavigationController *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
但是我的导航控制器仍然是 nil :-(
But my Navigation Controller is still nil :-(
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
NSLog(@"navContr: %@", myViewController.navigationController);
// -> "navContr: nil"
我也想在 NIB 文件中包含导航控制器,但 Apple 文档只说它不是最佳的"而没有实际解释如何做.我希望我的主选项卡式窗口调用包含典型导航控件的模式窗口,而无需以编程方式定义导航控制器.我将分享我是如何做到的.
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.
您的代码与我的非常相似,因为我还在 MyViewController 中定义了一个 IBOutlet UINavigationController *navigationController
.在 Interface Builder 中,我只是在根视图控制器中定义了我的视图,并将 File's Owner->view 设置为该视图定义.
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];
(我在 self 上调用 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 添加导航控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!