我有一个带有弹出框的应用程序。当我走出街灯时。我通过UIAlertController
解除了弹出窗口(用户回答是)。但是,在解雇popover之前,我正在调用委托上的函数。在该函数中是另一个UIAlertController
。由于以下错误,第二个UIAlertController
未显示:
不允许在取消分配时尝试加载视图控制器的视图,并且可能导致未定义的行为。
为了在此演示,我创建了一个快速项目来显示问题。它只是一个视图控制器,带有一个调用弹出窗口的按钮和一个关闭它的弹出窗口上的按钮,并调用包含另一个UIAlertController
的委托函数。
这是调用popover的视图控制器的代码:
//Delegate function called from popover
func doSomeStuff() {
let alert = UIAlertController(title: "Some Stuff", message: "Do you want to do some stuff", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: {action in
print("We did something here.")
}))
alert.addAction(UIAlertAction(title: "No", style: .Cancel, handler: nil))
presentViewController(alert, animated: true, completion: nil)
}
@IBAction func callPopover(sender: UIButton) {
let popoverVC = self.storyboard?.instantiateViewControllerWithIdentifier("PopoverView") as! PopoverController
popoverVC.modalPresentationStyle = UIModalPresentationStyle.Popover
popoverVC.preferredContentSize = CGSizeMake(200, 200)
if let popoverController = popoverVC.popoverPresentationController {
popoverController.backgroundColor = UIColor.lightGrayColor()
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
popoverController.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds),0,0)
popoverController.sourceView = callPopoverButton
popoverController.delegate = self
popoverVC.delegate = self
self.presentViewController(popoverVC, animated: true, completion: nil)
}
}
//Allows popover to present on devices besides iPad.
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle{
return UIModalPresentationStyle.None
}
callPopover函数是第一个屏幕上按钮的操作。
这是弹出窗口的代码:
var delegate: ViewController!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func returnToMainView(sender: UIButton) {
let alert = UIAlertController(title: "Dismiss Popover", message: "Do you want to dismiss this popover?", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: {action in
self.delegate.doSomeStuff()
self.dismissViewControllerAnimated(true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "No", style: .Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
点击第一个屏幕上的弹出框按钮时,弹出窗口会正确显示:
点击返回按钮会显示警告:
点击“是”从警报返回并显示第二个警报,但是我收到错误的位置。
我认为第二个警报没有显示,因为popover尚未被解雇,但不知道如何绕过它。
答案 0 :(得分:0)
我看到您正在从第一个正在关闭的弹出视图控制器中呈现第二个弹出视图控制器。这导致了这个问题。
相反,为什么你不能从导航控制器而不是第一个弹出视图控制器呈现第二个视图控制器。
在presentViewController(alert, animated: true, completion: nil)
方法中将此行self.navigartionController.presentViewController(alert, animated: true, completion: nil)
更改为doSomeStuff()
,以便第一个弹出窗口可以自由解散,导航控制器实际显示您的第二个弹出窗口。希望这有帮助!