警告:尝试出现< UIAlertController:0x7facd3946920> on< ...>已经呈现(null) [英] Warning: Attempt to present <UIAlertController: 0x7facd3946920> on <...> which is already presenting (null)

查看:553
本文介绍了警告:尝试出现< UIAlertController:0x7facd3946920> on< ...>已经呈现(null)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableView 上设置了长按手势,其中显示了包含单元格文本的 UIAlertController 。当出现 UIAlertController 时,我收到此警告:

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)

据我所知,MainTaskView( UITableView )已经呈现了一个视图,所以它不应该出现第二个视图, UIAlertController。所以我尝试了这个解决方案。它不起作用,因为我得到相同的警告。我该怎么做才能解决这个警告?请参阅下面的代码:

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

控制台输出:

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

出于某种原因,两段代码都是当长按手势发生时在if语句中执行。将显示警报,并将文本打印到控制台。这是一个问题吗?

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?

编辑:正如马特所说,我没有把我的所有代码都放在手势识别器测试的范围内。移动它解决我的问题。测试之外的代码被执行了两次,导致 UIAlertController 被呈现两次。

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.

推荐答案


出于某种原因,如果

这对我来说应该响起警钟。 if else 都不可能运行。此代码必须运行两次。

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.

这是因为您未测试手势识别器的状态。长按g.r.发送其动作消息两次。您在长按和发布时都运行此代码。你需要测试g.r的状态。所以你不要这样做。示例:

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

这篇关于警告:尝试出现&lt; UIAlertController:0x7facd3946920&gt; on&lt; ...&gt;已经呈现(null)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆