self.navigationController 分配为 RootViewController 后为 null?

发布于 2024-11-29 00:39:27 字数 1774 浏览 1 评论 0原文

我创建了一个新的 xcode 项目作为基于视图的应用程序,并且我有一组 UIViewController ,我计划在单独的 UINavigationController 中使用它们。

ParentViewController.m 中,在所有 UINavigationController 之前以及所有 myViewController 启动之后:

NSMutableArray *navControllers = [[NSMutableArray array];
for (id aVC in self.myViewControllers) {
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aVC];

    //[aVC setNavigationController:navController];
    [navController setNavigationBarHidden:YES];
    [navController setToolbarHidden:YES];
    [navControllers addObject:navController];
    [navController release];
}

_navigationControllers = [[NSArray arrayWithArray:navigationControllers] retain];

_navigationControllers 保留为 ParentViewController 的成员,所以我想在 for 循环内启动的所有导航控制器都由 _navigationControllers 保存,这样它们就不会被释放或变成 nil,但是当我尝试在MyViewController中使用navigationController来推送SomeOtherViewController,它不起作用:

- (IBAction)pushDetailView {
     [self.navigationController pushViewController:self.detailViewController animated:YES];
}

我在pushViewController:someOtherViewController之前放置了一个断点,并且“po [ self navigationController]”,控制台告诉我它是一个 nil 引用。

我以为当我这样做时 [[UINavigationController alloc] initWithRootViewController:aVC],底层机制会将navigationController分配为aVC.navigationController,因为Apple“视图控制器编程” iOS 指南”执行相同的操作,但无需将 navigationController 分配给 rootController

除非我取消标记 for 循环 //[aVC setNavigationController:navController]; 的第二行,否则 aVC 中不存在 navigationController

我是否误解了该机制?我的情况还有其他解决方案吗? 提前致谢!

I created a new xcode project as View-based application, and I have a set of UIViewController(s) which I plan to use inside separate UINavigationController(s).

In ParentViewController.m before all the UINavigationController(s) and after all myViewControllers been initiated:

NSMutableArray *navControllers = [[NSMutableArray array];
for (id aVC in self.myViewControllers) {
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aVC];

    //[aVC setNavigationController:navController];
    [navController setNavigationBarHidden:YES];
    [navController setToolbarHidden:YES];
    [navControllers addObject:navController];
    [navController release];
}

_navigationControllers = [[NSArray arrayWithArray:navigationControllers] retain];

_navigationControllers is retained as a member of ParentViewController, so I suppose all my navigation controllers initiated inside for-loop are kept by _navigationControllers so they won't be released or become nil, but when I try to use navigationController in MyViewController to push SomeOtherViewController, it doesn't work:

- (IBAction)pushDetailView {
     [self.navigationController pushViewController:self.detailViewController animated:YES];
}

I put a breakpoint before pushViewController:someOtherViewController, and "po [self navigationController]", the console tells me it is a nil reference.

I assumed that when I do
[[UINavigationController alloc] initWithRootViewController:aVC], the underlying mechanism would assign the navigationController as aVC.navigationController, because the Apple "View Controller Programming Guide for iOS" does the same without assigning navigationController to rootController.

Unless I unmark the second line of the for-loop //[aVC setNavigationController:navController];, the navigationController does not exist in aVC.

Am I misunderstanding the mechanism? Is there another solution for my case?
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

滴情不沾 2024-12-06 00:39:27
_navigationControllers = [NSMutableArray array];
for (id aVC in self.myViewControllers) {
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aVC];

    //[aVC setNavigationController:navController];
    [navController setNavigationBarHidden:YES];
    [navController setToolbarHidden:YES];
    [navControllers addObject:navController];
}

// assuming index 0 navigation controller is with 'ParentViewController'
self.rootViewController = [_navigationControllers objectAtIndex:0];

检查一下。

_navigationControllers = [NSMutableArray array];
for (id aVC in self.myViewControllers) {
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aVC];

    //[aVC setNavigationController:navController];
    [navController setNavigationBarHidden:YES];
    [navController setToolbarHidden:YES];
    [navControllers addObject:navController];
}

// assuming index 0 navigation controller is with 'ParentViewController'
self.rootViewController = [_navigationControllers objectAtIndex:0];

check with this.

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