0
votes

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

3

3 Answers

0
votes

You need not to initialise UIPopoverPresentationController, you can get reference from viewcontorller which you want to present as popover

@IBAction func showPopUP(sender: AnyObject) {

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

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

    var presentationController = popVC.popoverPresentationController  //Here, you can go with
    presentationController!.permittedArrowDirections = UIPopoverArrowDirection.Left | UIPopoverArrowDirection.Right
    presentationController!.sourceView = popVC.view
    presentationController!.sourceRect = popVC.view.frame

}   

Enjoy Coding !!

0
votes

You shouldn't be creating a new UIPopoverPresentayionController yourself, you should be requesting the controller from your presented controller:

var presentationController = popVC.popoverPresentationController

Also, when you do:

presentationController.sourceView = popVC.view
presentationController.sourceRect = popVC.view.frame

you're using the wrong view and frame. The view should be the view that the popover is presented from, not the popover view, and the rect is the area in the source view where the presentation if from (i.e. where the arrow comes from).

0
votes

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)


    }