UINavigationController 不显示根视图控制器
我在视图中有一个大小为 320x218 的 UIView(下面代码中的 menuView)。我想将导航控制器加载到该视图中。我使用以下代码来执行此操作:
MenuViewController *menuController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:menuController];
navigationController.navigationBarHidden = YES;
[menuView addSubview:navigationController.view];
[menuController release];
[navigationController release];
当我执行它时,根视图不会显示在该视图中。仅显示导航栏,视图的其余部分为空。
编辑:
我刚刚在 MenuViewController 的 initWithNibName: 和 viewDidLoad: 中放置了一个 NSLog() 。 initWithNibName: 中的那个被调用,但 viewDidLoad: 中的那个没有被调用:S
更新:
我试图将 menuController
推入我的 navigationController
思维因为它没有出现,所以它可能不在堆栈上。例外:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported
I have a UIView (menuView in code below) of size 320x218 inside a view. I want to load a navigation controller into this view. Im using the following code to do that:
MenuViewController *menuController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:menuController];
navigationController.navigationBarHidden = YES;
[menuView addSubview:navigationController.view];
[menuController release];
[navigationController release];
When I execute it, the root view is not displayed in that view. Only a navigation bar is displayed and the rest of the view is empty.
Edit:
I just placed an NSLog() in both initWithNibName: and viewDidLoad: of MenuViewController. The one in initWithNibName: gets called but the one in viewDidLoad: doesn't :S
Update:
I tried to push menuController
to my navigationController
thinking since its not appearing, it might not be on the stack. Exception:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
调用layoutsubviews确实有效。
call layoutsubviews do work.
我在这里找到了答案:
UIViewController -viewDidLoad not being called
我必须添加这些行
-initWithRootViewController
之后的代码,以便加载我的根视图控制器的视图:I found the answer here:
UIViewController -viewDidLoad not being called
I had to add these lines of code after
-initWithRootViewController
in order to load the view of my root view Controller:您不应该将 navigationViewController 作为子视图添加到 MenuViewController 中。
由于 navigationViewController 已经持有 MenuViewController。
只显示navigationViewController。
You should not add the navigationViewController as an subview To your MenuViewController.
As the navigationViewController already already holds the MenuViewController.
Just display the navigationViewController.
在您的 appdelegate 方法中尝试此代码
Try this code in your appdelegate method