2

The following code should work, right?

ViewController2 *childView = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
 [self.navigationController pushViewController:childView animated:YES];
 [childView release];

It doesn't do anything though. There are no error messages. The view just doesn't get switched. viewDidLoad does not even execute inside ViewController2.

1
  • Have you debugged it at all? Is childView nil?
    – Ben Zotto
    Sep 18, 2010 at 0:04

2 Answers 2

1

That code won't do anything if the view controller presenting it doesn't have a navigation controller, i.e. it isn't in a navigation controller stack. In that case, you'll be calling a method (pushViewController:animated:) on a nil object (self.navigationController) which does nothing. Thus, you can only use this this method if the "parent" view controller is in a UINavigationController stack.

4
  • I tried that code and it crashed saying unrecognized selector.
    – node ninja
    Sep 18, 2010 at 1:57
  • It's saying unrecognized selector because that class doesn't know about that method, which means self isn't a UIViewController or subclass. Where are you calling the code from? Please post more code so there's more context. (My gut tells me you're doing this from inside a view class.) Sep 18, 2010 at 2:33
  • I'm using a UIViewController but it's in a view-based application not a nav-based application.
    – node ninja
    Sep 18, 2010 at 3:34
  • 1
    Oh, oops. Yeah, that's the problem. You can only push view controllers onto a navigation stack. Sorry for that. I will amend my answer. Sep 18, 2010 at 3:56
0

Use this:

[self presentModalViewController:viewControllerNameHere animated:YES];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.