2017-01-08 71 views
0

我有一个应用程序弹出窗口。当我离开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的委托函数。

enter image description here

这是调用酥料饼视图控制器的代码:

//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) 
} 

当轻敲第一屏幕上的酥料饼的按钮时,酥料饼显示正确:

enter image description here

攻丝的返回按钮显示提醒:

enter image description here

单击是从警报返回并应显示第二个警报,但这是我得到错误的地方。

我认为第二个警报没有显示,因为popover还没有完成被解散,但不知道如何解决它。

+0

听起来像特别糟糕的用户体验。 – Mundi

回答

0

我看到您正在展示第二个弹出视图控制器从正在被解雇的第一个弹出视图控制器。这是造成这个问题。

相反,为什么不能从导航控制器而不是第一个弹出视图控制器显示第二个视图控制器。

在您的doSomeStuff()方法中将presentViewController(alert, animated: true, completion: nil)这一行更改为self.navigartionController.presentViewController(alert, animated: true, completion: nil),以便第一个弹出窗口可以自由消除,导航控制器实际上会显示第二个弹出窗口。希望这可以帮助!

+0

我试过了,它没有做任何不同的事情。仍然收到错误。仍然没有得到警报。 –

+0

似乎不可能在警报中执行警报。有意义,看到警报视图不是模态。 –