在Objective-C中从UIViewController显示一个SplitViewController

我正在开发一个ViewController(login应用程序)与一个button,当我按下这个button,我想出现我的UISplitView像这样:

- (IBAction)loadSplitViewController:(id)sender { [self showSplitViewController]; } 

并为我的splitViewController的创build开发的代码是这样的:

 -(void)showSplitViewController{ UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle: nil]; LeftViewController *leftViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"LeftViewController"]; RightViewController *rightViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"RightViewController"]; UINavigationController *leftNavController = [[UINavigationController alloc] initWithRootViewController:leftViewController]; UINavigationController *rightNavController = [[UINavigationController alloc] initWithRootViewController:rightViewController]; UISplitViewController *splitViewController = [[UISplitViewController alloc] init]; splitViewController.viewControllers = [NSArray arrayWithObjects:leftNavController, rightNavController, nil]; leftViewController.delegate = rightViewController; splitViewController.delegate = rightViewController; [self presentViewController:splitViewController animated:YES completion:^{}]; } 

事情是…如果我使用显示我的splitViewController这一行:

 [self presentViewController:splitViewController animated:YES completion:^{}]; 

抛出一个错误

我也试过了

 [self.view addSubview:splitViewController.view]; 

但这样我的splitViewController永远不会旋转,委托不工作以及…我不希望我的splitViewController是我的viewController的子视图,我希望它看起来更像一个独立的modalView

任何帮助,我会感激

提前致谢

分割视图控制器真的应该是窗口的根视图控制器(事实上苹果公司说,它必须是,虽然似乎有一些例子,这是不正确的)。 您可以将窗口的根视图控制器切换为分割视图控制器,而不是显示它。

 self.view.window.rootViewController = splitViewController;