App with nested UINavigationController crashes in

2019-06-01 11:12发布

问题:

I'm in the process of migrating an App to the current iOS SDK. Its root controller is UINavigationController with another UINavigationController as first view controller.

The application now crashes with EXC_BAD_ACCESS caused by some kind of infinite recursion, but I can't make sense of the strack trace. As far as I can tell, nothing has changed expect some project settings to make it compile on the current SDK.

I created a new Navigation-based Application and added a nested UINavigationController, which also crashed the application. Is nesting UINavigationControllers generally a bad idea?

Any hints what might be causing this or how to interpret the stack trace and debug the problem are greatly appreciated.

回答1:

It will work when you set the navigationbar of the parent navigation controller to hidden (in interface builder or in code) before loading the view:

navigationController.navigationBar.hidden = YES;
[self.window addSubview:navigationController.view];


回答2:

Well it does seem odd to have a nested UINavigationController, but heck what do I know. What is the root view controller of the nested nav controller? Hopefully not the top nav controller.



回答3:

Here it is:

  1. Create a new UINavigationController
  2. In viewDidLoad or viewWillLoadalloc and init the controller to whichever root you want
  3. Resize the NavigationController's view and add it as subview to the parent viewCOntroller's view (I have self.contentView)

BOOM!!

here is the code..

- (void)viewDidLoad
{
[super viewDidLoad];
[self.contentView setFrame:CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height)];

mNavController =[[UINavigationController alloc]initWithRootViewController:myNestedRootController]; /
[self.mNavController.view setFrame:CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height)];
[self.mNavController setNavigationBarHidden:YES]; //optional
[self.contentView addSubview:self.mNavController.view];
}

Actually you can skip the contentView and have the mNavController's view directly on viewcontroller's view (my bad)

I have a method to push (only push) a new VC

-(void)moveToContentViewController:(PM_BaseContentVC *)contentvc animated:(BOOL)animated{
[self.mNavController popToRootViewControllerAnimated:NO]; //silently pop the previous viewcontroller
[self.mNavController pushViewController:contentvc animated:YES]; //push new viewcontrolelr
[self.contentView addSubview:self.mNavController.view]; //add the view
}

sugestions and optimizations are welcome :)