在HalfSizePresentationController类

时间:2016-02-24 08:30:24

标签: swift

我尝试在另一个尺寸为半父视图控制器的viewcontroller上实现呈现模态视图控制器。

Present modal view controller in half size

当我运行此错误时显示:

致命错误:在展开Optional值时意外发现nil (lldb)

代码问题?

func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
    return HalfSizePresentationController(presentedViewController: presented, presentingViewController: presentingViewController!)
}

1 个答案:

答案 0 :(得分:1)

通常在你得到unexpectedly found nil...的情况下,罪魁祸首是你试图强行解开的东西!

在你的情况下唯一的东西!是presentingViewController所以这可能是一个开始寻找的好地方。

if let presenting = presenting {
    return HalfSizePresentationController(presentedViewController: presented, presentingViewController: presenting!)
}

实际上,当我开始仔细查看您的代码时,请查看您的签名:

func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?

您有一个presentingViewController外部参数名称,但内部参数名称为presenting,但是当您使用时,尝试使用presentingViewController代替presenting参数:

return HalfSizePresentationController(presentedViewController: presented, presentingViewController: presentingViewController!)

应该是:

return HalfSizePresentationController(presentedViewController: presented, presentingViewController: presenting!)

希望有所帮助