Warning: Attempt to present on <…> which is already presenting (null)

后端 未结 4 850
孤城傲影
孤城傲影 2021-02-07 02:00

I have a long press gesture set on a UITableView that presents a UIAlertController containing the cell\'s text. When the UIAlertController

相关标签:
4条回答
  • 2021-02-07 02:27

    I have had the same issue. I was able to fix it by this code:

            if self.presentedViewController == nil {
                self.present(Alert, animated: true, completion: nil)
            }
            else {
                self.dismiss(animated: false, completion: nil)
                self.present(Alert, animated: true, completion: nil)
            }
    
    0 讨论(0)
  • 2021-02-07 02:29

    0

    Dismiss the current controller and present the alert controller like

    func alert(_ message:String) {
    let alert = UIAlertController(title: "Error!", message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
    self.dismiss(animated: false, completion: nil)
    self.present(alert, animated: true,completion: nil)
    }
    
    0 讨论(0)
  • 2021-02-07 02:36

    For some reason, both pieces of code are executing in the if

    That should have rung alarm bells for me. It is impossible that both the if and the else should run. This code must be running twice.

    That is because you are not testing the state of the gesture recognizer. A long press g.r. sends its action message twice. You are running this code both on the long press and on the release. You need to test the state of the g.r. so that you don't do that. Example:

    @IBAction func longPressedView(g: UIGestureRecognizer) {
        if g.state == .Began {
            // ... do it all here
        }
    }
    
    0 讨论(0)
  • 2021-02-07 02:43

    You should differentiate the gesture state then execute the code you want, if not the selector you add to target will be executed first time when the gesture's state is UIGestureRecognizerStateBegan and second time when the gesture's state is UIGestureRecognizerStateCancelled, the second performance, alertController is showing, so Xcode will log warning.

    0 讨论(0)
提交回复
热议问题