【问题标题】:iOS application tried to push a nil view controller on target <UINavigationController:iOS 应用程序试图在目标 <UINavigationController: 上推送一个 nil 视图控制器:
【发布时间】:2015-06-04 09:00:28
【问题描述】:

XMPP 使用 tableview 我正在尝试使用特定用户名转到下一个视图控制器。所以为此我已经正确实现了 UItablevie 的didselectrowatIndexpath。但是当我点击用户名时会出现这样的错误。

应用程序试图在目标上推送一个 nil 视图控制器。

"。 2015-06-04 14:22:51.063

我已经像这样在 appdelegate.m 中声明了故事板。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
      ChatViewController *ChatViewController1 = [[ChatViewController alloc]init];
self.storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];


return YES;
}

这是我的 tableview 的 Delegate 方法。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
[self.tblvwbuddy deselectRowAtIndexPath:indexPath animated:YES];
XMPPUserCoreDataStorageObject *user = nil;
user = [[self fetchedResultsController] objectAtIndexPath:indexPath];

ChathistryViewController *chathistory1 = [[ChathistryViewController alloc] init]; // or initWith...
chathistory1 = [appdelegate.storyboard instantiateViewControllerWithIdentifier:@"ChathistryViewController"];//My next view controller's name
     chathistory1.chatUserObject = user;

      chathistory1.hidesBottomBarWhenPushed = YES;

chathistory1.self.tabBarController.navigationItem.title = user.displayName;

[self.tabBarController.navigationController pushViewController:chathistory1 animated:YES];

}

我缺少什么?还是我错了?

【问题讨论】:

  • 您的appdelegate.storyboardchathistory1 对象是nil。如果情节提要对象不为零,请确保情节提要中的场景与代码初始化中提到的标识符相同。
  • 这些故事板怎么会随机变为零?

标签: ios xcode uitableview


【解决方案1】:

试试这个:

 UITabBarController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"main"];
        NSArray *listViewController=vc.viewControllers;
        vc.selectedViewController=[listViewController objectAtIndex:1];
        [self presentViewController:vc animated:YES completion:nil];

ps.... listViewController objectAtIndex: 1

【讨论】:

  • 得到类似 >: Storyboard () 不包含标识符为“Main”的视图控制器
  • 您不能使用名称“main”,这是一个示例。您必须设置字段名称“Storyboard ID”,例如“main”,然后尝试运行代码。
【解决方案2】:

我猜你的故事板没有正确初始化,因为你遇到了错误。

不是在委托类中创建故事板对象,而是遵循以下方法来检索故事板对象。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

让我知道它是否有效。

【讨论】:

    【解决方案3】:

    编辑

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
         ChatViewController *ChatViewController1 = [[ChatViewController alloc]init];
         self.storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
         //HOW DID YOU DECLARE the rootViewController?
         /*
         you CANNOT do this: 
         self.window.rootViewController = ChatViewController1;
         */
    
         //you MUST have declared it like: right? becuase your are using navigationController
         UINavigationController *navigationView = self.storyboard instantiateViewControllerWithIdentifier:@"naviController"/*Storyboard ID*/];
         self.window.rootViewController = navigationView;
    
         //Manually setting navigationController rootViewController, example your rootViewController is ChatViewController
         UIViewController = self.storyboard instantiateViewControllerWithIdentifier:@"ChatViewController"/*Storyboard ID*/];
         [navigationView setViewControllers:UIViewController animated:NO];
    
         return YES;
    }
    

    结束编辑

    chathistory1 = [appdelegate.storyboard instantiateViewControllerWithIdentifier:@"ChathistryViewController"];troller
    

    您必须设置自定义类,在您的情况下:(ChathistryViewController)

    并确保您的 ChathistryViewController 作为 Storyboard ID 存在于您的 Storyboard 中

    来自您的代码:

    chathistory1 = [appdelegate.storyboard instantiateViewControllerWithIdentifier:@"ChathistryViewController"];
    

    我认为这样分配它会更好:

    //From your code [[ChathistryViewController alloc] init]; will be use less
    ChathistryViewController *chathistory1 = [[ChathistryViewController alloc] init]; 
    chathistory1 = [appdelegate.storyboard instantiateViewControllerWithIdentifier:@"ChathistryViewController"];
    

    更好的分配:

    ChathistryViewController *chathistory1 = [appdelegate.storyboard instantiateViewControllerWithIdentifier:@"ChathistryViewController"];
    

    另一个是你的[appdelegate.storyboard instantiateViewControllerWithIdentifier:];

    你在哪里声明了appdelegate

    您可以通过以下方式访问 AppDelegates 属性:

    ChathistryViewController *chathistory1 = [[UIApplication sharedApplication].storyboard instantiateViewControllerWithIdentifier:@"ChathistryViewController"];
    

    【讨论】:

    • UIViewController *navigationView = self.storyboard instantiateViewControllerWithIdentifier:@"naviController"/*Storyboard ID*/]; chathistry 不是我最初的视图控制器。
    • 当我写这个代码故事板集chathistry作为我最初的viewcontrollar。
    • 我已经设置好了。但是当我直接打开它时仍然显示chathistoryviewcontroller。
    • 您是否为chathistoryviewcontroller 取消选中is initial View Controller?我将编辑我的答案以手动分配 navigationController rootViewController..
    猜你喜欢
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多