我可以使 UINavigationController 仅在第二级加载,而不是在根视图控制器加载
我尝试寻找类似的问题,但找不到类似的问题。
我正在 UIView 中加载 UINavigationController,它不是(在大多数示例中)MainWindow。
我创建了一个名为 DocumentsViewController 的新 .xib,它是 UIView 的子类(它具有相关的 .m 和 .h 文件)。我创建了一个 DocumentsRootViewController.xib,它是 UITableViewController 的子类,它应该是 UINavigationController 的 RootViewController。
我移至 DocumentsViewController 并在 Interface Builder 中添加了 UINavigationController 对象。然后我开始编写代码,将其添加到 IBOutlet 中,并将其连接到该对象。
在 ViewDidLoad 中,我执行以下几行:
DocumentsRootViewController *rootViewController = [[[DocumentsRootViewController alloc] init] autorelease];
rootViewController.title = @"Documents";
[navigationControllerDocuments initWithRootViewController:rootViewController];
[self.view addSubview:navigationControllerDocuments.view];
它按预期显示表格,但显示“根视图控制器”的“后退”按钮(如下图所示)。
为什么?它不是应该已经知道rootviewcontroller已经设置了吗?
提前感谢那些澄清这个疑问的人
Giovanni
I tried looking for a similar problem but I could not find a similar question.
I am loading a UINavigationController in a UIView which is not (as in most examples) the MainWindow.
I created one new .xib called DocumentsViewController which is subclass of UIView (it has the related .m and .h files). And I created a DocumentsRootViewController.xib, which is a subclass of UITableViewController, which is supposed to be the UINavigationController's RootViewController.
I moved to DocumentsViewController and added a UINavigationController object in Interface Builder. Then I went to code, and added it as in IBOutlet, and connected it to the object.
In the ViewDidLoad, I execute the following lines:
DocumentsRootViewController *rootViewController = [[[DocumentsRootViewController alloc] init] autorelease];
rootViewController.title = @"Documents";
[navigationControllerDocuments initWithRootViewController:rootViewController];
[self.view addSubview:navigationControllerDocuments.view];
It shows the table as intended, but it shows a "Back" button to the "Root View Controller" (as in picture below).
Why? Shouldn't it already know that the rootviewcontroller has been set?
Thank you in advance to the ones that clarify this doubt
Giovanni
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您通过 Nib 添加 UINavigationController 时,它实际上会在 nib 文件内创建一个 UINavigationController 实例,其中包含默认 RootViewController 集(类型为 UIViewController)以及默认标题 RootViewController。
当您加载 nib 时,该对象将作为加载 nib 的一部分创建(即,当您初始化 DocumentsViewController 时) - 因此 navigationControllerDocuments 出口已初始化为 UINavigationController,并已在其上设置了默认 ViewController。
我认为正在发生的事情是当您调用“initWithRootViewController”时 - 您正在一个已经初始化的对象上调用它 - 因此它再次运行初始化代码 - 将第二个视图控制器(DocumentRootViewController)推入堆栈,但默认的视图控制器在笔尖中创建的内容已经存在。
您可能应该做的就是忘记在笔尖中创建一个并以编程方式初始化整个事情。
即你在哪里做:
我建议你做一个分配和初始化:
因为你这样做,你真的不需要将导航控制器添加到笔尖,所以如果这有效,你应该从笔尖中删除它,因为你是在代码中将其替换为这个。
When you add the UINavigationController via the Nib it actually creates an instance of a UINavigationController inside the nib file with a default RootViewController set (of type UIViewController) and with a default title of RootViewController.
When you load the nib, this object is being created as part of loading the nib (i.e when you initialise DocumentsViewController) - so the navigationControllerDocuments outlet is already initialised as a UINavigationController with the default ViewController already set on it.
What I think is happening is when you call 'initWithRootViewController' - you are calling this on an already initialised object - so it is running the initialisation code again - pushing the second view controller (the DocumentRootViewController) onto the stack, but the default one that was created in the nib is already there.
What you should probably do is forget about creating one in the nib and initialise the whole thing programatically.
i.e. where you do:
I suggest that you do an alloc and init instead:
Since you are doing this you really don't need to have the navigation controller added to the nib so if this works you should remove it from the nib since you are replacing it with this one in code.