ios - 弹出窗口和 UIAlertController 错误

标签 ios swift popover uialertcontroller

我有一个带有弹出窗口的应用程序。当我从弹出窗口中出来时。我通过 UIAlertController 关闭弹出窗口(用户回答是)。不过,在关闭弹出窗口之前,我在委托(delegate)上调用一个函数。该函数中有另一个 UIAlertController。由于以下错误,第二个 UIAlertController 未显示:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior.

为了在这里演示,我创建了一个显示问题的快速项目。它只是一个 View Controller ,带有一个调用弹出窗口的按钮和弹出窗口上的一个按钮,用于关闭它并调用包含另一个 UIAlertController 的委托(delegate)函数。

enter image description here

这是调用弹出窗口的 View Controller 的代码:

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

单击"is"将从警报中返回,并应显示第二个警报,但这就是我收到错误的地方。

我认为第二个警报没有显示,因为弹出窗口尚未完成关闭,但不知道如何解决它。

最佳答案

我看到您正在从第一个弹出 View Controller 呈现第二个弹出 View Controller ,该 Controller 正在被关闭。这就是导致问题的原因。

为什么不能从导航 Controller 显示第二个 View Controller 而不是第一个弹出 View Controller 。

将这行presentViewController(alert,animated:true,completion:nil)更改为self.navigartionController.presentViewController(alert,animated:true,completion:nil)在您的 doSomeStuff() 方法中,以便第一个弹出窗口可以自由关闭,并且导航 Controller 实际上呈现您的第二个弹出窗口。希望这有帮助!

关于ios - 弹出窗口和 UIAlertController 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41528010/

相关文章:

Swift 应用程序未从初始 ViewController 场景开始

ios - 第一次应用程序更新,用户数据丢失(存储在 Documents 目录中)

ios - 如何通过 Storyboard加载 nib 文件?

ios - 使用核心数据计算多节表的总和

swift - Alamofire - NSURLCache 不工作?

arrays - 在 Swift 中使用过滤器时如何处理元素位置

javascript - Bootstrap3 popover data-trigger=focus 在弹出窗口中单击 <select> 输入时关闭弹出窗口

jquery - 表单和输入未显示在 Bootstrap 弹出窗口中

reactjs - 如何动态定位 IonPopover 以从按钮出现?

ios - AWS Cognito 用户池 - "Value null at ' userName' 未能满足约束 : Member must not be null"- iOS