从TabBarController中保留SplitViewController

时间:2016-10-06 15:06:23

标签: ios uisplitviewcontroller

UISplitViewController内有UITabBarController。现在,当我单击一个按钮时,我想将这两个控制器放在后面并显示另一个ViewController。

我正在使用此代码:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
    RegistrationViewController *vc = (RegistrationViewController *)[storyboard instantiateViewControllerWithIdentifier:@"RegistrationViewController"];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = vc;
}

现在,RegistrationViewController会弹出一秒钟,但会立即消失。如何让RegistrationViewController留下来?

1 个答案:

答案 0 :(得分:0)

Per Apple docs:

  

注意

     

" ...尽管可以在某些其他容器视图控制器中将拆分视图控制器安装为子级,但在大多数情况下不建议这样做。拆分视图控制器通常安装在应用程序窗口的根目录下。有关实现界面的方法的提示和指导,请参阅iOS人机界面指南。"

如果不这样做,您可能会遇到意外行为(并拒绝App Store提交)。

UITabBarController嵌入UISplitViewController.

的详细信息视图控制器可能会更好

UISplitViewController .preferredDisplayMode设置为.primaryHidden(隐藏左侧1/3'主视图',为您提供UITabBarController的全屏显示。)然后,您可以致电showDetailViewController来展示您的RegistrationViewController

FWIW,我很幸运地嵌入了两个UINavigationController' Master'和'细节' UISplitViewController的推送和弹出视图,以显示导航堆栈。这里有很棒的教程,有关使用NavControllers的好处的更多讨论: https://www.raywenderlich.com/94443/uisplitviewcontroller-tutorial-getting-started

一些Swift代码:

(使用上述方法,将导航控制器作为主控和详细信息,仅显示UISplitViewController,的详细视图,然后将新视图控制器显示为详细视图。)

@IBAction func createAccountButtonSelected(_ sender: AnyObject) {
    let registrationVC = self.storyboard?.instantiateViewController(withIdentifier: "RegistrationViewController") as! RegistrationViewController
    self.navigationController?.pushViewController(registrationVC, animated: true)
}

在另一种情况下,我想重置UISplitViewController的Master和Detail视图的导航堆栈(来自Detail View Controller):

let firstVC = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController
let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    // Get a reference the the Master View nav controller
let masterNav = self.splitViewController!.viewControllers[0] as! UINavigationController
    // Set desired VC's as nav stack RootViewControllers        
self.navigationController?.setViewControllers([matchVC], animated: true)
masterNav.setViewControllers([masterMatchTableView], animated: true)