ios - UINavigationController - 应用程序试图在目标上推送一个 nil View Controller ,我错过了什么?

标签 ios uinavigationcontroller didselectrowatindexpath

我在让我的导航 Controller 正常运行时遇到了问题!如果我在 RootViewController 的表格单元格中单击,它似乎不是下一个 ViewController。

错误信息读取

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

所以我分配了一些错误,这是我的猜测,我可能遗漏了我所关注的书中的一些重要内容。

所以问题出现在我的 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;
    } 
}

在我的 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;
}

因此,当我在 Cell 中单击时,我得到了我想要推送的所有其他 ViewController。我只是看不出我的错或我错过了什么!?

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

最佳答案

RootViewController 已经有了它的导航 Controller - 您在应用委托(delegate)中创建了它。选择单元格时创建新的导航 Controller 没有意义。它应该看起来更像这样:

- (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;
    }
}

此外,只需确保当您调用 -pushViewController:animated: View Controller (即 kundleViewConkalenderViewCon wunschViewCon) 是非零的。它们看起来像实例变量或属性 - 只需确保您在代码中较早地分配/初始化它们,例如 -viewDidLoad

关于ios - UINavigationController - 应用程序试图在目标上推送一个 nil View Controller ,我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8393246/

相关文章:

ios - 以编程方式快速切换另一个导航

objective-c - 调用 didSelectRowAtIndexPath 时激活单元格的附属 View (UISwitch)

ios - XCode 8 未列出任何设备

iphone - 什么事件会重置 backgroundTimeRemaining?

ios - 在没有导航 Controller 的情况下向代码中的 View 添加导航栏

iphone - 无法为 subview 推送ViewController

长按选中的 Swift 3.0 Table View Cell

ios - 从 Facebook 应用程序打开我的应用程序

javascript - ios - 如何在 Web View 中使用 javascript 打开快速 View ?

ios - UINavigationController 堆栈中的阴影