【发布时间】:2016-06-14 20:51:50
【问题描述】:
我有一个从 ViewController A 到 ViewController B 的 segue。它使用自定义 segue 类 RightSegue。这是 RightSegue 类的代码:
class RightSegue: UIStoryboardSegue {
override func perform() {
// Assign source and destination view controllers to local variables.
let vc1: UIViewController = self.sourceViewController
let vc2: UIViewController = self.destinationViewController
// Get screen width and height.
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
// get the source view controller's frame.
let vc1Frame = vc1.view.frame
let vc2NavController = vc2.navigationController
let vc2NavFrameHeight = vc2NavController!.navigationBar.frame.size.height
// Specify the initial position of the destination view. (x, y, width, height)
vc2.view.frame = CGRectMake(screenWidth, vc2NavFrameHeight, screenWidth, screenHeight)
// Push the destinationViewController onto sourceViewController.
vc1.navigationController!.pushViewController(vc2, animated: false)
// Specify the initial position of the source view. Add destination view as a subview of the source view.
vc1.view.frame = CGRectMake(vc1.view.frame.origin.x, vc1.view.frame.origin.y, screenWidth, vc1Frame.size.height)
vc2.navigationController?.view!.addSubview(vc1.view!)
// Animate!
UIView.animateWithDuration(0.25, animations: {() -> Void in
vc1.view.frame = CGRectMake(-screenWidth, vc1.view.frame.origin.y, vc1.view.frame.size.width, vc1.view.frame.size.height)
vc2.view.frame = CGRectMake(0.0, 0.0, vc2.view.frame.size.width, vc2.view.frame.size.height)
}, completion: {(finished: Bool) -> Void in
vc1.view!.removeFromSuperview()
})
}
视图控制器的设置如图所示。
可以看出,FirstViewController 包含一个按钮,单击该按钮会显示 SecondViewController。
下一张图片显示了 segue 的属性:
当我执行应用程序并按下 FirstViewController 中的按钮时,应用程序崩溃并显示错误消息:
fatal error: unexpectedly found nil while unwrapping an Optional value
我在 RightSegue 类中检查了vc2NavController。它的返回类型为UINavigationController?。强制解包返回 nil。
如此答案 (Custom Segue in Swift) 中所示,我添加了 @objc() 并运行了应用程序。它仍然没有工作。应用程序崩溃,这次给出错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not create a segue of class '(null)''
我不知道该怎么办。请帮忙。
【问题讨论】:
标签: ios uiviewcontroller swift2 segue uistoryboardsegue