1

我正在尝试使用 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];to[self.navigationController popViewControllerAnimated:YES];但结果保持不变,没有任何反应。

我的结论是:我在'nextView'的self.navigationController部分做错了,即程序不知道从/到什么推/弹出。我已尽力查看有关此的某种文档,但我无法解决,所以我在这里。

self.navigationController 是正确的方法还是我在该行中遗漏了什么?

提前谢谢你,托拜厄斯·托维达尔

4

2 回答 2

1

当您使用self.navigationController时,您不会访问aNavController在 showSettings 中定义的导航控制器。self.navigationController指本端视图控制器可能被推送的 Navigation。我假设定义此代码的视图没有 NavigationController。这意味着self.navigationController指向null。您可以通过在 nextView 方法中验证这一点NSLog(@"%@", self.navigationController);

您需要做的是在您的头文件中将 aNavController 设为实例变量,然后您可以[aNavController pushViewController:thirdView animated:YES];在 nextView: 方法中执行此操作。

于 2011-08-26T14:37:09.380 回答
0

你的问题似乎是

UINavigationController *aNavController = [[UINavigationController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:aNavController animated:YES];

将一个新的模型视图控制器推送到堆栈,并且不使这个导航控制器成为当前视图控制器的导航控制器;

所以您需要将当前视图控制器导航控制器设置为aNavController 或将aNavController 设为类变量并使用它来推送第三个视图。

于 2011-08-26T14:38:44.850 回答