Search code examples
iosswiftpopover

Popover View is Blank


I am trying to add a popover to my controller using code. For some reason, the popover appears, but there is no content inside.

enter image description here

The code I'm using for the transition is:

@IBAction func presentPopover(_ sender: UIButton) {
    //performSegueWithIdentifier("Popover", sender: self)
    let vc = PopoverViewController()
    vc.modalPresentationStyle = .popover
    let popover = vc.popoverPresentationController!
    popover.delegate = self
    popover.permittedArrowDirections = .right
    vc.popoverPresentationController?.sourceView = sender
    vc.popoverPresentationController?.sourceRect = sender.bounds
    ...
    present(vc, animated: true, completion: nil)
}

The popover view is made in the storyboard, and is of class PopoverViewController After doing some testing it says that the PopoverViewController's viewDidAppear is triggered.

enter image description here


Solution

  • as rmaddy mention The line let vc = PopoverViewController() does not use the storyboard. so you need to do it like that

    if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopoverViewController") as? PopoverViewController {
        vc.modalPresentationStyle = .popover
        let popover = vc.popoverPresentationController!
        popover.delegate = self
        popover.permittedArrowDirections = .right
        vc.popoverPresentationController?.sourceView = sender
        vc.popoverPresentationController?.sourceRect = sender.bounds                        
        self.present(vc, animated: true, completion: nil)
    }