Swift 从非viewcontroller的自定义类以编程方式调用PopoverPresentationController
我试图从不是viewcontroller的自定义类中呈现popoverpresentationcontroller,但这似乎不起作用。除了为popover创建的viewcontroller之外,我只有一个viewcontroller。我想通过程序实现这一点。有什么想法吗 以下是我的示例代码:Swift 从非viewcontroller的自定义类以编程方式调用PopoverPresentationController,swift,ios8,Swift,Ios8,我试图从不是viewcontroller的自定义类中呈现popoverpresentationcontroller,但这似乎不起作用。除了为popover创建的viewcontroller之外,我只有一个viewcontroller。我想通过程序实现这一点。有什么想法吗 以下是我的示例代码: func showErrorPopup() { let storyboard = UIStoryboard(name: "Main", bundle: nil) let parentView
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)
}
}
在引用视图控制器的非viewcontroller中创建一个委托。谢谢@JohnRiselvato,它工作得很好。只是一个注释,我们尝试了观察者和通知,这很有效,但我们倾向于委托模式。感谢您的回复。您是否尝试过UIApplication.sharedApplication().rootViewController来处理演示文稿?