我相信将第二个导航视图控制器呈现为模态视图控制器会更好。例如,将您当前的 presentController 选择器替换为以下内容:
- (void)presentController:(id)sender {
ChildViewController1 *cvc = [[ChildViewController1 alloc] initWithNibName:@"ChildViewController1" bundle:nil];
nc3 = [[UINavigationController alloc] initWithRootViewController:cvc];
nc3.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:nc3 animated:YES completion:nil];
UIBarButtonItem *i = [[UIBarButtonItem alloc] initWithTitle:@"X" style:UIBarButtonItemStyleBordered target:self action:@selector(close)];
cvc.navigationItem.leftBarButtonItem = i;
}
然后,您的关闭选择器可能变为:
- (void)close {
[nc3 dismissViewControllerAnimated:YES completion:nil];
}
(虽然我建议移动按钮的创建并实际在 ChildViewController1.m 中处理关闭)。
当然,这将使导航控制器的所有创建脱离 ViewController.m 的 viewDidLoad 选择器:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(0, 100, 100, 40);
[b setTitle:@"present" forState:UIControlStateNormal];
[b addTarget:self action:@selector(presentController:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b];
}
希望它有效!