user1396737
user1396737

Reputation: 15

Not able to push view controller

I am trying to push a simple view onto my current view but it doesn't seem to work.

In my app delegate I did:

self.window.backgroundColor = [UIColor whiteColor];
__firstVC = [[FirstPageViewController alloc]initWithNibName:nil bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:__firstVC];
[self.window addSubview:[navController view]];
[self.window makeKeyAndVisible]; Return YES;

and then in my first view controller:

SecondPageViewController *secondView = [[SecondPageViewController alloc] init];
    [secondView setTitle:@"What do you want?"];
    [self.navigationController pushViewController:secondView animated:YES];

but nothing seems to happen. I put a nslog statement in the init of secondviewcontroller and I can see that, but no view. Can someone help?

Upvotes: 1

Views: 1323

Answers (1)

sc0rp10n
sc0rp10n

Reputation: 1118

Your navigation controller has no owner. When you reach the end of your didFinishLaunchingWithOptions method in the app delegate, the navigation controller will be deallocated. Instead of adding its view to the window, set it as the root view controller:

self.window.rootViewController = navController;

EDIT: I guess the statement above may not be true depending on whether or not your using ARC. Without ARC you'd simply be leaking the nav controller which would probably be fine since you'll be using it throughout the life of the app.

Upvotes: 1

Related Questions