Search code examples
iosswiftdelegatesuipopovercontroller

Delegation to Presenting VC, From a Popover


I dragged a ViewController onto my storyboard, segued to it with a popover segue, and setup the size and style in the presentingVC's prepareForSegue. My question is, if my popover has several buttons, where should their code be executed?

  1. For example, should I use a delegation pattern where, in the prepareForSegue, I pass the delegation reference as self? Then delegate backward?
  2. Or, should I create a new viewController for the popover, then put the code to be run in there?
  3. I also read this tutorial and someone said...

" And you get hold of the the content controller by using the popoverPresentationController.presentedViewController method in the UIPopoverPresentationController

What would be ideal for me, since the code I want will change some presentingVC variables, would be delegate back to the presenting VC.


Solution

  • Ultimately just went with delegation in the competition block of the dismiss popover call:

    class NavigationViewController: UIViewController {
    
        var presentingVC_Delegate: mainLists_PopoverDelegation!
        var whatToDo = "Placeholder"
    
        @IBOutlet var shareBtn: UIButton!
        @IBOutlet var clearBtn: UIButton!
        @IBOutlet var settingsBtn: UIButton!
    
        //***** ----- ***** ------ ***** ----- ***** ----- *****
        //Menu Button Functions
        //***** ----- ***** ------ ***** ----- ***** ----- *****
        @IBAction func shareBtn_Pressed(sender: AnyObject) {
            self.dismissViewControllerAnimated(true, completion: { finished in
                self.presentingVC_Delegate.call_ActivityVC()
            })
        }
        @IBAction func clearBtn_Pressed(sender: AnyObject) {
            self.dismissViewControllerAnimated(true, completion: { finished in
                self.presentingVC_Delegate.deleteList()
            })
        }
        @IBAction func settingsBtn_Pressed(sender: AnyObject) {
            self.dismissViewControllerAnimated(true, completion: { finished in
                self.presentingVC_Delegate.presentSettingsVC()
            })
        }
    }
    
    protocol mainLists_PopoverDelegation {
      func call_ActivityVC ()
      func deleteList ()
      func presentSettingsVC ()
    }
    

    With those three functions located in the main VC.