ios 如何使用 Interface Builder 添加导航控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4670528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a Navigation Controller with Interface Builder?
提问by Manni
1. Step:Create a new UIViewController:
- Xcode -> New File... -> Cocoa Touch Class -> UIViewController
- Name: MyViewController
1、步骤:新建一个UIViewController:
-Xcode->New File...->Cocoa Touch
Class- >UIViewController-名称:MyViewController
2. Step:Drag and drop a "Navigation Controller" (UINavigationController) from the Library to MyViewController.xib
2. 步骤:从库中拖放一个“导航控制器”(UINavigationController)到 MyViewController.xib
3.Step:I'm sure, I have to do something to connect the Navigation Controller correctly, isn't it?
3.Step:我确定,我必须做一些事情才能正确连接导航控制器,不是吗?
4.Step:Try to start the new View Controller as a modal dialog:
4.Step:尝试以模态对话框的形式启动新的视图控制器:
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
NSLog(@"navContr: %@", myViewController.navigationController);
[self.navigationController presentModalViewController: myViewController animated:YES];
Result: "navContr: nil"
结果:“navContr:nil”
5.Step:You can see the new modal view (MyViewController), but there's no NavigationController and no UINavigationBar.
5.Step:可以看到新的模态视图(MyViewController),但是没有NavigationController和UINavigationBar。
Thank you very much for your help!
非常感谢您的帮助!
UPDATE 1:
更新1:
6.Step:I set a new UIViewController (ViewNavi2) as "Root View Controller":
6.Step:我将一个新的 UIViewController (ViewNavi2) 设置为“Root View Controller”:
7.Step:I define a IBOutlet UINavigationController *navigationController
in the class MyViewController and configure the xib: Navigation Controller -> Connections -> Referencing Outlets
7.Step:我IBOutlet UINavigationController *navigationController
在类MyViewController中定义了一个并配置了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"
回答by Henry
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.
我还想在 NIB 文件中包含导航控制器,但 Apple 文档所说的只是“不是最佳的”,而没有实际解释如何做到这一点。我希望我的主选项卡式窗口调用包含典型导航控件的模式窗口,而无需以编程方式定义导航控制器。我将分享我是如何做到的。
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.
您的代码与我的非常相似,因为我也在IBOutlet UINavigationController *navigationController
MyViewController 中定义了一个。在 Interface Builder 中,我只是在根视图控制器中定义了我的视图,并将 File's Owner->view 设置为该视图定义。
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:
(我在 self 上调用 presentModelViewController,我不知道你为什么有self.navigationController
。)当我测试这个时,结果是我的视图是从 NIB 文件加载的,但导航栏不见了。MyViewController 中的另一行代码修复了这个问题:
- (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.
我在这里所做的只是将 MyViewController 的视图设置为指向 myNavigationController.view,其中包含 NIB 文件中定义的所有内容,包括导航控制器。
回答by Walter
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.
查看 XCode 中的模板以获取“基于导航的应用程序”。(New Project -> Navigation-based Application) 然后在 Interface Builder 中打开 MainWindow.xib。虽然它不是视图,但可以将其视为您在步骤 1 中创建的视图控制器。然后查看检查器中引用的“根视图控制器”,看看它是如何连接到“根视图控制器”笔尖的属性检查器和身份检查器中的“RootViewController”类。
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.
您需要将要显示的内容放在您的“RootViewController”版本中的导航根目录中。通常,接下来显示的控制器是“DetailViewController”。再次通读模板提供的示例代码,您将看到如何开始。查看didSelectRowAtIndexPath
RootViewController.m 的示例模板中的注释代码。您将需要创建自己的视图控制器作为 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.
//根据您的评论进行编辑//
Apple 文档说,如果您想使用 IB 来制作您的导航控制器,您应该真正将它们放在 MainWindow.xib 中。我正在修补您不能将 NavigationController 作为 UIView 对象的子视图这一事实。根据您从哪里开始,您可以做的是使用 IB 将导航栏对象添加到您的视图控制器,然后从那里控制事物。
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."
来自 Apple 文档“虽然您也可以从您的主 nib 文件(或任何其他 nib 文件)加载独立或模态呈现的导航控制器,但这样做并不是最佳选择。在这些情况下,通常更容易以编程方式创建导航控制器使用点。”
//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"
//更多编辑//在XCode4中,项目模板现在称为“主细节”项目。这是因为它将在 iPad 中设置为拆分视图控制器,但在 iPhone 中仍然是常规导航项目。我在答案中引用“RootViewController”的地方,你现在会找到一个叫做“MasterViewController”的东西
回答by Oleg Danu
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.
原因是因为 UIViewController 的 navigationController 属性仅在视图控制器在其堆栈中时才返回导航控制器。如果找不到导航控制器,则此属性为零。您的视图控制器不在导航控制器的堆栈中。无论如何,我什至不确定您是否正确初始化了我们的导航控制器。阅读一些文档。