[英]Error: Cannot convert value of type '(_, _) -> Void' to expected argument type '((UIAlertAction) -> Void)?'
I have a button created in my Main.storyboard
, which has a name (a var
) named deleteButton
. 我有我创建了一个按钮
Main.storyboard
,其中有一个名称( var
命名) deleteButton
。 This button is in each cell. 该按钮在每个单元格中。
When the user presses the button, an alert pops up to confirm the deletion. 当用户按下按钮时,弹出警报以确认删除。 Here is the code.
这是代码。
@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)
}
In the cellForRowAtIndexPath:
method, I have declared this line: 在
cellForRowAtIndexPath:
方法中,我声明了这一行:
cell.deleteButton.tag = indexPath.row
I get the error on the 3rd line (starting with alertController.addAction
) saying: 我在第三行收到错误(以
alertController.addAction
):
Cannot convert value of type '(_, _) -> Void' to expected argument type '((UIAlertAction) -> Void)?'
无法将类型'(_,_)-> Void'的值转换为预期的参数类型'((UIAlertAction)-> Void)?
How do I fix this? 我该如何解决?
The handler's parameter of UIAlertAction
constructor, takes the selected action object as its only parameter. UIAlertAction
构造函数的处理程序参数将选定的操作对象作为其唯一参数。
Instead of this: 代替这个:
alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (action, indexPath) -> Void in
you will want this: 您将需要:
alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction) -> Void in
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.