我在Main.storyboard
创建了一个按钮,其名称(var
)名为deleteButton
。此按钮位于每个单元格中。
当用户按下按钮时,会弹出警报以确认删除。这是代码。
@IBAction func deletePinpoint(sender: UIButton) {
let alertController = UIAlertController(title: "Delete pinpoint", message: "Do you want to delete this pinpoint?", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (action, indexPath) -> Void in
if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
let pinpointToDelete = self.fetchResultController.objectAtIndexPath(sender.tag) as! Pinpoints
managedObjectContext.deleteObject(pinpointToDelete)
do {
try managedObjectContext.save()
} catch {
print(error)
}
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
在cellForRowAtIndexPath:
方法中,我已经声明了这一行:
cell.deleteButton.tag = indexPath.row
我在第3行(从alertController.addAction
开始)收到错误说:
无法转换类型'(_,_) - >的值虚空'预期参数类型'((UIAlertAction) - > Void)?'
我该如何解决这个问题?
答案 0 :(得分:3)
UIAlertAction
构造函数的处理程序参数,将所选操作对象作为其唯一参数。
而不是:
alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (action, indexPath) -> Void in
你会想要这个:
alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction) -> Void in