Display Name
Display Name

Reputation: 1035

Popovers in Storyboard, how to handle the delegate

In a storyboard i have a view controller which has a segue to another viewcontroller, with the "Present As Popover" applied. If i don't add any code, this works as id expect.

Issue: I need to get the delegate working so i can get data back from the popover. I have created the delegate in the popover.

I have added

class LoginView: UIViewController,UIPopoverPresentationControllerDelegate, UIPopoverControllerDelegate,KeypadDelegate

I have added the functions to my main view controller.

Issue i have is how to set the delegate on the segue.

i have tried

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "KeyPadLoad"
        {
            let popoverViewController = segue.destinationViewController as! UIViewController
            popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
            popoverViewController.popoverPresentationController!.delegate = self
        }
    }

The segue is defiantly called KeyPadLoad but when the delegate functions should be called in the popover nothing happens.

What am i doing wrong

Thanks

Upvotes: 2

Views: 5841

Answers (1)

Victor Sigler
Victor Sigler

Reputation: 23451

You have to implement the UIPopoverPresentationControllerDelegate in the class that presents the Popover. The methods of the UIPopoverPresentationControllerDelegate protocol let you customize the behavior of a popover-based presentation.

If you have set a segue to go to the Popover and you set the segue as Present as Popover then your class has to be like this :

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {

   override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
   }

   override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
   }

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

       var popOver = segue.destinationViewController as! PopOverViewController
       popOver.popoverPresentationController!.delegate = self        
   }

   // Tells the delegate that the popover was dismissed.  
   func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
       println("dismissed")
   }
}

Where the class PopOverViewController is just an UIViewController that handle the Popover, no more, something like this code :

class PopOverViewController: UIViewController {

   override func viewDidLoad() {
      super.viewDidLoad()

      // Do any additional setup after loading the view.
   }

   override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
   }
 }

With the above code you should see the message:

dismissed

Every time that the PopOverViewController is dismissed.

I hope this help you.

Upvotes: 5

Related Questions