UINavigationController - 应用程序试图在目标上推送一个nil视图控制器,我缺少什么?

jul*_*dry 3 uinavigationcontroller didselectrowatindexpath ios

我有问题让我的导航控制器正常运行!如果我在RootViewController中单击表格的单元格,它似乎不是下一个ViewController.

错误消息读取

"应用程序试图将nil视图控制器推向目标."

所以我分配了一些错误的东西,我的猜测,我可能遗漏了我所遵循的书中的重要内容.

所以问题出现在我的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;
    } 
}
Run Code Online (Sandbox Code Playgroud)

在我的AppDelegate.m中,我正在做以下事情来将RootViewController设置为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;
}
Run Code Online (Sandbox Code Playgroud)

所以当我点击Cell时,我得到了我想要推送的所有其他ViewControllers.我只是看不出自己的错或我错过了什么!?

也许有人可以帮助我并给我一个提示!那太好了!

Stu*_*art 5

RootViewController已经有了它的导航控制器 - 你在app delegate中创建了它.选择单元格时创建新的导航控制器没有意义.它应该看起来更像这样:

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,只要确保当您调用-pushViewController:animated:视图控制器(即kundleViewCon,kalenderViewCon&wunschViewCon)是非零.它们看起来像实例变量或属性 - 只需确保在代码中早先分配/启动它们,例如-viewDidLoad.