如何在 Swift 中替换 UISplitViewController 中的 Detail?

How to replace Detail in UISplitViewController in Swift?

通常我可以用 Objective-C 中的代码替换我的 splitview 的详细视图:

  [self.splitViewController viewWillDisappear:YES];
  NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:[[self.splitViewController.viewControllers objectAtIndex:1] viewControllers]];
  [viewControllerArray removeLastObject];

  [viewControllerArray addObject:self.detailViewController];
  [[self.splitViewController.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];
  [self.splitViewController viewWillAppear:YES];

尝试在Swift中重写它,但我什至无法在这段代码中获取viewControllers:

  NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:[[self.splitViewController.viewControllers objectAtIndex:1] viewControllers]];

Swift:

    let test: UIViewController = (self.splitViewController?.viewControllers[1])!

它应该有 test.viewControllers ,但它没有如下图所示

Swift 是否以不同的方式处理 splitview 中的替换?

这对我有用,但我不确定这是否是最佳做法:

    let detailViewController = self.storyboard?.instantiateViewControllerWithIdentifier("DetailNavigationViewController") as! UINavigationController
    self.splitViewController?.viewControllers[1] = detailViewController

只需要得到想要的viewController并赋值给splitViewController的索引1