UINavigationController - Application tried to push a nil view controller on target, what am I missing?

julesmummdry picture julesmummdry · Dec 6, 2011 · Viewed 9.8k times · Source

I am having problems to get my Navigation Controller to run properly! If I click in the cell of the table at the RootViewController, it appears not the next ViewController.

Error message reads

“Application tried to push a nil view controller on target .”

So I alloc something wrong, was my guessing, I probably missing something important from the book I follow.

So the problem appears here in my RootViewController.m:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UINavigationController *navigationController= [[UINavigationController alloc]init];

    switch ([indexPath row]) {

        case 0:
            [navigationController pushViewController:kundeViewCon animated:YES];
            break;

        case 1:
            [navigationController pushViewController:kalenderViewCon animated:YES];
            break;

        case 2:
            [navigationController pushViewController:wunschViewCon animated:YES];
            break;
    } 
}

In my AppDelegate.m I am doing the following things to set a RootViewController as the NavigationController:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

// Navigation Controller

    RootViewController *rootViewController = [[RootViewController alloc]init];

    navigationController = [[UINavigationController alloc]initWithRootViewController:rootViewController];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

So I got all my other ViewControllers that I want to push, when I click in the Cell. I just can not see my fault or what I am missing!?

Maybe someone can help me and give me a hint! That would be great!

Answer

Stuart picture Stuart · Dec 6, 2011

RootViewController already has its navigation controller - you created it in the app delegate. Creating a new navigation controller when you select a cell doesn't make sense. It should probably look more like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch ([indexPath row]) {
        case 0:
            [self.navigationController pushViewController:kundeViewCon animated:YES];
            break;
        case 1:
            [self.navigationController pushViewController:kalenderViewCon animated:YES];
            break;
        case 2:
            [self.navigationController pushViewController:wunschViewCon animated:YES];
            break;
    }
}

Also, just make sure that when you call -pushViewController:animated: the view controller (i.e. kundleViewCon, kalenderViewCon & wunschViewCon) are non-nil. They look like instance variables or properies - just make sure you are alloc/initing them earlier in your code, like in -viewDidLoad.