我在让我的导航 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 (即 kundleViewCon
、kalenderViewCon
和 wunschViewCon
) 是非零的。它们看起来像实例变量或属性 - 只需确保您在代码中较早地分配/初始化它们,例如 -viewDidLoad
。
关于ios - UINavigationController - 应用程序试图在目标上推送一个 nil View Controller ,我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8393246/