如何显示ios8的默认PopOVer?

时间:2015-04-22 06:45:06

标签: ios swift popup uipopovercontroller

我正在尝试使用swift作为

在ios8中显示popover
    @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

    }
}

但是给我一个错误

  

由于未捕获的异常终止应用程序' NSGenericException',   原因:' - [UIPopoverController init]不是有效的初始值设定项。您   必须调用 - [UIPopoverController initWithContentViewController:]。'

我在这里做错了什么?

编辑:  我想显示默认情况下的弹出窗口8

enter image description here

以下是GoogleDrive上项目的链接: https://drive.google.com/open?id=0B6dTvD1JbkgBM3F6RXhjVGFvZmM&authuser=0

2 个答案:

答案 0 :(得分:0)

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

var presentationController = popVC.popoverPresentationController

此外,当你这样做时:

presentationController.sourceView = popVC.view
presentationController.sourceRect = popVC.view.frame
你正在使用错误的视图和框架。视图应该是弹出窗口的视图,而不是弹出视图,而rect是源视图中的区域,如果来自(即箭头来自的位置)。

答案 1 :(得分:0)

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

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

这告诉你的iphone强行将VC显示为popover。

并以与

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


    }