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

孤街醉人 提交于 2019-11-27 15:45:52

问题


I have a long press gesture set on a UITableView that presents a UIAlertController containing the cell's text. When the UIAlertController is presented I get this warning:

Attempt to present <UIAlertController: 0x7fd57384e8e0>  on <TaskAppV2.MainTaskView: 0x7fd571701150> which is already presenting (null)

From my understanding, MainTaskView (the UITableView) is already presenting a view, so it shouldn't present a second view, the UIAlertController. So I tried this solution from a similar question. It does not work as I get the same warning. What can I do to solve this warning? See below for code:

func longPressedView(gestureRecognizer: UIGestureRecognizer){

    /*Get cell info from where user tapped*/
    if (gestureRecognizer.state == UIGestureRecognizerState.Ended) {
        var tapLocation: CGPoint = gestureRecognizer.locationInView(self.tableView)

        var tappedIndexPath: NSIndexPath? = self.tableView.indexPathForRowAtPoint(tapLocation)
        if (tappedIndexPath != nil) {
            var tappedCell: UITableViewCell? = self.tableView.cellForRowAtIndexPath(tappedIndexPath!)
            println("the cell task name is \(tappedCell!.textLabel!.text!)")
        } else {
            println("You didn't tap on a cell")
        }
    }

    /*Long press alert*/
    let tapAlert = UIAlertController(title: "Long Pressed", message: "You just long pressed the long press view", preferredStyle: UIAlertControllerStyle.Alert)
    tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
    /*
    if (self.presentedViewController == nil) {
        self.presentViewController(tapAlert, animated: true, completion: nil)
    } else {
        println("already presenting a view")
    } */

    self.presentViewController(tapAlert, animated: true, completion: nil)
    println("presented")
}

Console output:

presented
You didn't tap on a cell
2015-05-19 22:46:35.692 TaskAppV2[60765:3235207] Warning: Attempt to present <UIAlertController: 0x7fc689e05d80>  on <TaskAppV2.MainTaskView: 0x7fc689fc33f0> which is already presenting (null)
presented

For some reason, both pieces of code are executing in the if statement when the long press gesture happens. The alert is presented and the text is printed to the console. Is this an issue?

Edit: As Matt said, I didn't have all my code in the scope of the gesture recognizer test. Moving that in fixed my problem. The code outside of the test was being executed twice, resulting in the UIAlertController being presented twice.


回答1:


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



回答2:


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



回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/30338709/warning-attempt-to-present-uialertcontroller-0x7facd3946920-on-which-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!