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];开发者_JAVA技巧
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!
_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.
精彩评论