0

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

Community
  • 1
  • 1

1 Answers1

0
let nav = UINavigationController(rootViewController: roc)

Here you are creating a brand new navigation controller with the root VC as the segue's destination controller. This isn't going to work - the destination controller is going to be set up with whatever container it needs, you don't create new view controllers in prepareForSegue.

You want to look at the popoverPresentationController of rvc, not of some navigation controller that isn't going to be added to the screen:

if (sender as! UIButton == btnRedeem) {
      let rvc = segue.destinationViewController as! RedeemViewController
      let popover = rvc.popoverPresentationController
      popover!.delegate = self

}

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thanks for helping jrturton however I still get ***"fatal error: unexpectedly found nil while unwrapping an Optional value"*** pointing to `popover!.delegate = self` – Floris de Vries Jun 29 '16 at 14:25
  • If that's the case then there is no popover presentation controller. Does this view controller actually appear in a popover when you hit that button? Are you working on the right segue? – jrturton Jun 29 '16 at 21:19
  • The RedeemViewController does actually appear and it's the correct segue. In the if statement it works as well if I state which segue instead of which button so referring to the segue is not the problem. As in: `if (segue.identifier == "segueToRedeemPopover") {//do stuff}` The seque is named and has Popover as type. The RedeemViewController is just an ordinary UIViewController `class RedeemViewController: UIViewController, UITextFieldDelegate { }` – Floris de Vries Jun 30 '16 at 22:13