views:

38

answers:

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  A: 

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.

Shaggy Frog
I tried that code and it crashed saying unrecognized selector.
awakeFromNib
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.)
Shaggy Frog
I'm using a UIViewController but it's in a view-based application not a nav-based application.
awakeFromNib
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.
Shaggy Frog
A: 

Use this:

[self presentModalViewController:viewControllerNameHere animated:YES];
XenElement