4

How can I pass data back from a PopoverViewController to the main view controller on an iPhone?

I know I'm doing something terribly wrong but I cannot figure it out.

Here is the code: PopoverViewController.swift

protocol PopoverViewControllerDelegate {
    func messageData(data: AnyObject)
}

class PopoverViewController: UIViewController {
    @IBOutlet weak var inputMessage: UITextField!
    var delegate: PopoverViewControllerDelegate?

    @IBAction func sendData(sender: AnyObject) {
        if inputMessage.text != ""{
            self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
            self.delegate?.messageData(inputMessage.text!)
        }
    }
}

Main ViewController.swift:

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate, PopoverViewControllerDelegate {

    @IBOutlet weak var showData: UILabel!

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // popover segue
        if segue.identifier == "popoverSegue" {

            let popoverViewController = segue.destinationViewController
            popoverViewController.popoverPresentationController!.delegate = self
        }
        // code to comunicate with data in popoverViewController
        let pvc = storyboard?.instantiateViewControllerWithIdentifier("popoverViewController") as! PopoverViewController
        pvc.delegate = self
        self.presentViewController(pvc, animated:false, completion:nil)
    }

    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }

    func messageData(data: AnyObject) {
        self.showData.text = "\(data)"
    }
}

With the code above I can pass data back to the main view controller without a problem, the issue is that the popover doesn't work, it just acts like a regular ViewController occupying the whole screen.

The funny thing is that if I comment the following line of code the popover works but I can no longer pass data back, I can see the popover but the passing data stops working.

// if I comment this line 
self.presentViewController(pvc, animated:false, completion:nil)

I don't get any errors, one just stops working.

Any suggestions?

Thanks a lot

vacawama
  • 150,663
  • 30
  • 266
  • 294
fs_tigre
  • 10,650
  • 13
  • 73
  • 146

1 Answers1

2

In prepareForSegue, the destinationViewController is your PopoverViewController. You need to cast it to that and set the delegate on that so that you can pass back data, and you need to set the popoverPesentationController?.delegate as well. You don't need the rest of the code in prepareForSegue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // popover segue
    if segue.identifier == "popoverSegue" {
        let popoverViewController = segue.destinationViewController as! PopoverViewController
        popoverViewController.delegate = self
        popoverViewController.popoverPresentationController?.delegate = self
    }
}
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • @ vacawama - The popover doesn't work, the passing data works but the popover viewController fills the entire screen. Thanks – fs_tigre Jul 24 '16 at 21:08
  • 1
    Did you select `presentAsPopover` when you wired up the segue in the Storyboard? – vacawama Jul 24 '16 at 21:10
  • 1
    What device are you testing on? – vacawama Jul 24 '16 at 21:12
  • iPhone 6s simulator – fs_tigre Jul 24 '16 at 21:13
  • I see, it works on the iPad. Unfortunately I need this to work on iPhone, sorry I didn't clarify that at the beginning. Original code works on iPhone. – fs_tigre Jul 24 '16 at 21:15
  • This piece of code works on iPhone `if segue.identifier == "popoverSegue" { let popoverViewController = segue.destinationViewController popoverViewController.popoverPresentationController!.delegate = self }` – fs_tigre Jul 24 '16 at 21:18
  • I would also suggest you make `messageData` take a `String` instead of `AnyObject`. – vacawama Jul 24 '16 at 21:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118162/discussion-between-fs-tigre-and-vacawama). – fs_tigre Jul 24 '16 at 21:36