我试图得到一种"从左到右"分层导航,与UINavigationController。我使用以下代码:
-(IBAction)showSettings:(id)sender {
UINavigationController *aNavController = [[UINavigationController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:aNavController animated:YES];
SecondView *secondView = [[SecondView alloc] initWithNibName:nil bundle:nil];
[aNavController pushViewController:secondView animated:NO];
UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithTitle:@"Knapp" style:UIBarButtonItemStylePlain target:self action:@selector(nextView:)];
aNavController.topViewController.navigationItem.rightBarButtonItem = aButton;
[aButton release];
[secondView release];
[aNavController release]; }
-(IBAction)nextView:(id)sender {
ThirdView *thirdView = [[ThirdView alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:thirdView animated:YES];
NSLog(@"Push it like it's hot"); }
'nextView'被正确调用,"Push it like it's hot"被打印在"Output"中,但是没有任何东西被推送,没有新视图显示。我也试过改变:从[self.navigationController pushViewController:thirdView animated:YES];
到[self.navigationController popViewControllerAnimated:YES];
,但是结果保持不变,什么都没有发生
是自我。navigationController的正确方法还是我遗漏了什么?
提前谢谢你,托拜厄斯Tovedal
当您使用self.navigationController
时,您没有访问在showSettings中定义为aNavController
的导航控制器。self.navigationController
指的是self视图控制器可能被推送到的导航。我假设定义这段代码的视图没有NavigationController。这意味着self.navigationController
指向null。您可以在nextView方法中验证这一点,执行NSLog(@"%@", self.navigationController);
你需要做的是使aNavController一个实例变量在你的头文件,然后你可以做[aNavController pushViewController:thirdView animated:YES];
在你的nextView:方法。
您的问题是
UINavigationController *aNavController = [[UINavigationController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:aNavController animated:YES];
将一个新的模型视图控制器推送到栈中,并且不使这个导航控制器成为当前视图控制器的导航控制器;
所以你需要将当前视图控制器的导航控制器设置为aNavController或者将aNavController设置为一个类变量并使用它来推送thirdView