self.navigationController 分配为 RootViewController 后为 null?
我创建了一个新的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查一下。
check with this.