I'm trying to get a sort of "left to right" hierarchical navigation going, with the UINavigationController. I'm using this code:

-(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' is called correctly, the "Push it like it's hot" is printed in "Output", but nothing is pushed, no new view shows up. I've also tried changing: [self.navigationController pushViewController:thirdView animated:YES]; to [self.navigationController popViewControllerAnimated:YES]; but the result stays the same, nothing happens.

My conclusion is this: I'm doing something wrong in the self.navigationController part of 'nextView', i.e the program doesn't know from/to what to push/pop. I've done my best to look at some sort of documentation about this but I can't work it out, so here I am.

Is self.navigationController the right way to do it or am I missing something in that line?

Thank you in advance, Tobias Tovedal

有帮助吗?

解决方案

When you are using self.navigationController, you are not accessing the navigation controller defined as aNavController in showSettings. self.navigationController refers to a Navigation that the self view controller may be pushed on. I assume that the view where this code is defined doesn't have a NavigationController. This means self.navigationController is pointing to null. You can verify this in the nextView method by doing NSLog(@"%@", self.navigationController);

What you need to do is make aNavController an Instance Variable in your Header File and then you can do [aNavController pushViewController:thirdView animated:YES]; in your nextView: method.

其他提示

What appears to be your problem is

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

pushes a new model view controller to the stack and does not make this navigation controller the current view controller's navigation controller;

so you need to set the current view controllers navigation controller to aNavController or make aNavController a class variable and use it to push the thirdView.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top