我有一个应用程序弹出窗口。当我离开popover时。我通过UIAlertController
解雇了popover(用户回答是)。然而,在解散popover之前,我正在调用委托的函数。在那个函数内是另一个UIAlertController
。第二UIAlertController
不是因为有下列错误显示:弹出窗口和UIAlertController错误
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior.
要在这里演示这一点,我创建了一个快速的项目,显示的问题。它只是一个带有按钮的视图控制器,该按钮调用弹出窗口中的按钮并关闭它并调用包含另一个UIAlertController
的委托函数。
这是调用酥料饼视图控制器的代码:
//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还没有完成被解散,但不知道如何解决它。
听起来像特别糟糕的用户体验。 – Mundi