简体   繁体   English

UINavigationController未显示Root View控制器

[英]UINavigationController not showing the Root View controller

I have a UIView (menuView in code below) of size 320x218 inside a view. 我在视图中有一个大小为320x218的UIView(下面的代码中的menuView)。 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. 我刚刚在MenuViewController的initWithNibName:和viewDidLoad中放置了一个NSLog()。 The one in initWithNibName: gets called but the one in viewDidLoad: doesn't :S initWithNibName:中的那个被调用但viewDidLoad中的那个:不是:S

Update: 更新:

I tried to push menuController to my navigationController thinking since its not appearing, it might not be on the stack. 我试图将menuControllermenuController我的navigationController思路,因为它没有出现,它可能不在堆栈上。 Exception: 例外:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported

call layoutsubviews do work. call layoutsubviews可以正常工作。

[super loadView];
[self.view addSubview:navigationController.view];
[navigationController.view layoutSubviews];

I found the answer here: 我在这里找到了答案:

UIViewController -viewDidLoad not being called UIViewController -viewDidLoad未被调用

I had to add these lines of code after -initWithRootViewController in order to load the view of my root view Controller: 我必须在-initWithRootViewController之后添加这些代码行,以便加载我的根视图控制器的视图:

navigationController.navigationBarHidden = YES;
[navigationController setView:menuController.view];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.

ViewController *viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

navController = [[UINavigationController alloc]initWithRootViewController:viewController];

self.window.rootViewController = self.navController;

Try this code in your appdelegate method 在你的appdelegate方法中试试这段代码

You should not add the navigationViewController as an subview To your MenuViewController. 您不应将navigationViewController作为子视图添加到MenuViewController。 As the navigationViewController already already holds the MenuViewController. 由于navigationViewController已经拥有MenuViewController。

Just display the navigationViewController. 只需显示navigationViewController。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM