如何在Swift中调用`setViewControllers:animated:`?

时间:2014-11-12 11:38:01

标签: ios swift uinavigationcontroller

当用户点击按钮时,我尝试在导航控制器中更改我的视图控制器,因此我声明了以下代码:

if standingsViewController == nil {
   standingsViewController = StandingsViewController()
   splitViewController!.delegate = standingsViewController
}
var vc = splitViewController!.viewControllers[1] as UINavigationController
vc.setViewControllers([standingsViewController], animated: true)

但是,这会导致错误:fatal error: attempt to bridge an implicitly unwrapped optional containing nil在最后一行。

UINavigationController' setViewControllers: animated:方法在Swift中正确定义,那么如何解决问题?

当我尝试将其更改为[standingsViewController]!时,为了获取您的信息,它甚至无法通过编译,因为[AnyObject] is not identical to [AnyObject]!

我在Swift中使用Xcode 6.1 beta。

1 个答案:

答案 0 :(得分:7)

看起来你忘了打开包装:

vc.setViewControllers([standingsViewController!], animated: true)

而不是

vc.setViewControllers([standingsViewController], animated: true)