我试图从一个不是viewcontroller的自定义类中呈现一个popoverpresentationcontroller,但这似乎不起作用。除了我为popover创建的viewcontroller之外,我只有一个viewcontroller。我想以编程方式实现这一目标。有什么想法吗?
以下是我的示例代码:
func showErrorPopup() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let parentViewController = UIApplication.sharedApplication().keyWindow?.rootViewController
if let contentView = storyboard.instantiateViewControllerWithIdentifier("ErrorPopUpView") as? ErrorViewController {
contentView.modalPresentationStyle = .Popover
contentView.preferredContentSize = CGSizeMake(400.0, 500.0)
if let _popoverPresentationController = contentView.popoverPresentationController {
_popoverPresentationController.delegate = self
_popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.Any
_popoverPresentationController.sourceView = parentViewController?.view
_popoverPresentationController.sourceRect = CGRectMake(-30, -280, 320, 400)
parentViewController?.presentViewController(contentView, animated: true, completion: nil)
}
}