0
votes

I tried to implement presenting modal view controller on the other viewcontroller sized to half parent view controller.

Present modal view controller in half size

when i run this error is show :

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

the code problem ?

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

1 Answers

1
votes

Normally in cases where you get unexpectedly found nil... the culprit is something you try to force unwrap with !

In your case the only thing with a ! is presentingViewController so that could be a good place to start looking.

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

And actually, as I start to look closer at your code, look at your signature:

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

You have a presentingViewController external parameter name, but the internal parameter name is presenting, and yet you try to use the presentingViewController instead of presenting when you use your parameters:

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

Should be:

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

Hope that helps