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

StackOverflow https://stackoverflow.com/questions/8393246

سؤال

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!

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

You don't have to create a new UINavigationController. You need to get you controller from the current window.

[self.navigationController pushViewController:yourController animated:YES];

where yourController is one of your instantiated UIViewController (kundeViewCon, kalenderViewCon or wunschViewCon)

For Googlers:

This may also happen if you did not connect your ViewController to the:

"<Your Project> App Delegate"

If you don't do it then your controller des not get initialized.

You also need to rename the class of the ViewController to the corresponding .h / .m file:

Open the xib file -> select your ViewController -> go to the "Identity Inspector" -> Type in the Textfield "Class" the name of your corresponding .h/.m files.

Connect your ViewController to the App By right click and drag from the ...AppDelegate to your ViewController enter image description here After releasing click on the corresponding entry: enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top