错误:无法转换类型'(_,_) - >的值虚空'预期的论证类型'((UIAlertAction) - > Void)?'

时间:2016-04-23 20:11:29

标签: ios swift delete-row uialertcontroller

我在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)?'

我该如何解决这个问题?

1 个答案:

答案 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