Swift 2.0 中的自定义 Segue:destinationViewController 的 navigationController 属性 返回 nil
Custom Segue in Swift 2.0 : destinationViewController's navigationController property returning nil
我有一个从 ViewController A 到 ViewController B 的转场。它使用自定义转场 class RightSegue。这是 RightSegue class:
的代码
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 class 中检查了 vc2NavController
。它有一个 return 类型的 UINavigationController?
。强制展开它 returns nil.
如本回答 (Custom Segue in Swift) 所示,我添加了 @objc() 和 运行 应用程序。它仍然没有用。应用程序崩溃,这次给出错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not create a segue of class '(null)''
我不知道该怎么办。请帮忙
编辑:
你需要推动你的第二个 viewController 让它有一个 navigationController。
自定义转场是导致您出现问题的原因。你需要 return 导航控制器,或者更好 - 阅读这个:
http://netsplit.com/custom-ios-segues-transitions-and-animations-the-right-way
如果您想保留习俗,这可能会有所帮助 -
Custom Push Segue removes navigation bar and tab bar in story board
我有一个从 ViewController A 到 ViewController B 的转场。它使用自定义转场 class RightSegue。这是 RightSegue class:
的代码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 class 中检查了 vc2NavController
。它有一个 return 类型的 UINavigationController?
。强制展开它 returns nil.
如本回答 (Custom Segue in Swift) 所示,我添加了 @objc() 和 运行 应用程序。它仍然没有用。应用程序崩溃,这次给出错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not create a segue of class '(null)''
我不知道该怎么办。请帮忙
编辑: 你需要推动你的第二个 viewController 让它有一个 navigationController。 自定义转场是导致您出现问题的原因。你需要 return 导航控制器,或者更好 - 阅读这个:
http://netsplit.com/custom-ios-segues-transitions-and-animations-the-right-way
如果您想保留习俗,这可能会有所帮助 - Custom Push Segue removes navigation bar and tab bar in story board