UINavigationController 不显示根视图控制器

发布于 2024-11-27 08:35:04 字数 1036 浏览 2 评论 0原文

我在视图中有一个大小为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末的微笑 2024-12-04 08:35:04

调用layoutsubviews确实有效。

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

call layoutsubviews do work.

[super loadView];
[self.view addSubview:navigationController.view];
[navigationController.view layoutSubviews];
南街女流氓 2024-12-04 08:35:04

我在这里找到了答案:

UIViewController -viewDidLoad not being called

我必须添加这些行-initWithRootViewController 之后的代码,以便加载我的根视图控制器的视图:

navigationController.navigationBarHidden = YES;
[navigationController setView:menuController.view];

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:

navigationController.navigationBarHidden = YES;
[navigationController setView:menuController.view];
ら栖息 2024-12-04 08:35:04

您不应该将 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.

莫相离 2024-12-04 08:35:04
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;

在您的 appdelegate 方法中尝试此代码

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文