我在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呈现两次.