【发布时间】:2015-05-20 01:19:25
【问题描述】:
我在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, 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 出现两次。
【问题讨论】:
-
能看到相关代码吗?视图 controllers 呈现其他 controllers,因此您的 UITableView 应该不是问题。看来您有点混淆了视图和控制器。
-
您链接到的问题的公认答案似乎很清楚。您可以在您的问题中添加一些相关代码吗?
-
好的,给我几分钟。
-
"这行代码使警告静音" 因为它完全不同。而不是解雇现有的提出的v.c.并呈现一个新的,你现在有 两个 呈现的视图控制器一个在另一个之上。如果这就是你想要的,那很好。但请确保它是。
-
我认为这是因为长按表格单元格会召唤菜单。你和那个有冲突。我在下面的回答可能让我们摆脱困境。
标签: ios uitableview swift uialertcontroller