Search code examples
iosswiftpopupuipopovercontroller

How to display default PopOVer of ios8?


i am trying to show popover in ios8 using swift as

    @IBAction func showPopUP(sender: AnyObject) {

        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let popVC = storyboard.instantiateViewControllerWithIdentifier("pop")as! PopViewController

        popVC.modalPresentationStyle = UIModalPresentationStyle.Popover
        self.presentViewController(popVC, animated: true, completion: nil)

        var presentationController = UIPopoverPresentationController()
        presentationController.permittedArrowDirections = UIPopoverArrowDirection.Left | UIPopoverArrowDirection.Right
        presentationController.sourceView = popVC.view
        presentationController.sourceRect = popVC.view.frame

    }
}

However gives me an error as

Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController init] is not a valid initializer. You must call -[UIPopoverController initWithContentViewController:].'

What am i doing wrong here?

EDIT: i want to show popover as of the default of ios 8

enter image description here

Here is the link of project on GoogleDrive : https://drive.google.com/open?id=0B6dTvD1JbkgBM3F6RXhjVGFvZmM&authuser=0


Solution

  • well first of all you set the Deleagte of UIPopoverPresentationControllerDelegate to self. then implement this method as

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

    This tells your iphone forcefully show the VC as popover.

    and implement your IBAction same way as

     @IBAction func showPopUP(sender: AnyObject) {
    
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)            
            let popVC = storyboard.instantiateViewControllerWithIdentifier("pop")as! PopViewController            
            popVC.modalPresentationStyle = UIModalPresentationStyle.Popover
            popVC.popoverPresentationController!.delegate = self
            let popOverController = popVC.popoverPresentationController
            popOverController!.sourceView = sender as! UIView
            popOverController!.sourceRect = sender.bounds
            popOverController?.permittedArrowDirections = .Any
            self.presentViewController(popVC, animated: true, completion: nil)
    
    
        }