Swift 2 - 打开分割视图控制器

时间:2015-10-11 21:39:49

标签: swift controller segue swift2 uisplitviewcontroller

我的iOS应用中有UISplitViewController,但我的初始视图控制器是正常的UIViewController

我想在UISplitViewController点击按钮时打开UIViewController

@IBAction func openSplitViewController(sender: AnyObject) {
    let splitViewController = UISplitViewController()

    let leftNavController = splitViewController.viewControllers.first as! UINavigationController
    let masterViewController = leftNavController.topViewController as! MenuTableViewController

    let rightNavController = splitViewController.viewControllers.last as! UINavigationController
    let detailViewController = rightNavController.topViewController as! DetailViewController

    splitViewController.viewControllers = [masterViewController,detailViewController];

    self.presentViewController(splitViewController, animated: true, completion: nil)
}

但是当我点击按钮时,我得到:

fatal error: unexpectedly found nil while unwrapping an Optional value

我应该如何打开UISplitViewController中的UIViewController

1 个答案:

答案 0 :(得分:0)

您的错误与UISplitViewController无关,您得到的错误是因为您的一个或多个参数为nil

因此,请确保您创建的所有viewController都是真实的而不是零。

查看此行的实例:

let leftNavController = splitViewController.viewControllers.first as! UINavigationController

此时您的splitViewController未分配viewController,因此leftNavController将为零。在将navController添加到viewController之前,您需要先创建splitViewController@EnableAutoConfiguration(exclude = RepositoryRestMvcAutoConfiguration.class)