ios – 警告:尝试在已经呈现的<...>上呈现(null)

我在UITableView上设置了一个长按手势,呈现一个包含单元格文本的UIAlertController.当UIAlertController被呈现时,我得到这个警告:
Attempt to present <UIAlertController: 0x7fd57384e8e0>  on <TaskAppV2.MainTaskView: 0x7fd571701150> which is already presenting (null)

从我的理解,MainTaskView(UITableView)已经呈现一个视图,所以它不应该呈现第二个视图,UIAlertController.所以我从一个类似的问题尝试了this解决方案.它不工作,因为我得到相同的警告.我可以做些什么来解决这个警告?见下面的代码:

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,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语句中执行.提示警报并将文本打印到控制台.这是个问题吗?

编辑:正如Matt所说,我没有我的所有代码在手势识别器测试的范围内.移动这固定我的问题.测试之外的代码正在执行两次,导致UIAlertController呈现两次.

解决方法

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

那应该是响了我的闹钟. if和else都应该运行是不可能的.此代码必须运行两次.

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

@IBAction func longPressedView(g: UIGestureRecognizer) {
    if g.state == .Began {
        // ... do it all here
    }
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...