我在 a 上设置了长按手势UITableView
呈现出一个UIAlertController
包含单元格的文本。当。。。的时候UIAlertController
出现后我收到此警告:
Attempt to present <UIAlertController: 0x7fd57384e8e0> on <TaskAppV2.MainTaskView: 0x7fd571701150> which is already presenting (null)
根据我的理解,MainTaskView(UITableView
) 已经呈现了一个视图,因此它不应该呈现第二个视图,即UIAlertController.
所以我尝试了this https://stackoverflow.com/q/28270282/3711622来自类似问题的解决方案。它不起作用,因为我收到同样的警告。我该怎么做才能解决这个警告?请参阅下面的代码:
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 语句中执行。将显示警报并将文本打印到控制台。这是一个问题吗?
编辑:正如马特所说,我的所有代码都没有在手势识别器测试的范围内。移动它解决了我的问题。测试之外的代码被执行了两次,导致UIAlertController
被呈现两次。