I have a problem assigning a Delegate to a PopoverPresentationController using Swift 2.2 in Xcode 7.3
Reason of using a Delegate is I'm trying to trigger a function when pressing outside of a Popover view (class RedeemViewController) to go back to the main menu (class MenuViewController). This happens without a button. The function exists in MenuViewController, but it's not relevant what it does now, so I just included the Delegation part where the error occurs.
class MenuViewController: UIViewController, UIPopoverPresentationControllerDelegate {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (sender as! UIButton == btnRedeem) {
let rvc = segue.destinationViewController as! RedeemViewController
let nav = UINavigationController(rootViewController: rvc)
let popover = nav.popoverPresentationController
popover!.delegate = self
}
}
}
Pushing the button btnRedeem, connected to the Segue towards the Popover view, RedeemViewController, leads to
fatal error: unexpectedly found nil while unwrapping an Optional value
In Xcode, the debugger points to
popover!.delegate = self
The Segue exists and is named in the Storyboard. There are no warnings or obsolete references when right-clicking on the view.
Replacing the code with
popover?.delegate = self
leads to a more generic error where the debugger stops in AppDelegate:
libc++abi.dylib: terminating with uncaught exception of type NSException
I looked here but none of the possible causes applies. I have a generic AppDelegate.swift as described here.
Any help is appreciated! I mostly found examples with buttons but do not want to use a button to exit from the Popover since it's not needed.
Additional info on the Delegation: the function I need running on the background is
popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController)
to check when the Popover view (RedeemViewController) is exited and the function I want to trigger within it is one that reveals a new button.
Thank you for reading and maybe you can help me out!
Cheers,
Floris