如何显示ios8的默认PopOVer?

How to display default PopOVer of ios8?

我正在尝试使用 swift 作为

在 ios8 中显示弹出窗口
    @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

    }
}

但是给我一个错误

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

我做错了什么?

编辑: 我想显示默认值 ios 8

的弹出窗口

这是 GoogleDrive 上的 link 项目: https://drive.google.com/open?id=0B6dTvD1JbkgBM3F6RXhjVGFvZmM&authuser=0

您不应该自己创建一个新的 UIPopoverPresentayionController,您应该从您提供的控制器请求控制器:

var presentationController = popVC.popoverPresentationController

此外,当您这样做时:

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

您使用了错误的视图和框架。视图应该是显示弹出窗口的视图,而不是弹出窗口视图,并且 rect 是源视图中显示的区域(即箭头的来源)。

您无需初始化 UIPopoverPresentationController,您可以从 viewcontorller 获取您想要呈现为弹出窗口的参考

@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

}   

享受编码!!

首先,您将 UIPopoverPresentationControllerDelegate 的 Deleagte 设置为自己。然后将此方法实现为

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

这告诉您 iphone 强制显示 VC 作为弹出窗口。

并以与

相同的方式实施您的 IBAction
 @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)


    }