簡體   English   中英

錯誤:無法將類型'(_,_)-> Void'的值轉換為預期的參數類型'(((UIAlertAction)-> Void)?

[英]Error: Cannot convert value of type '(_, _) -> Void' to expected argument type '((UIAlertAction) -> Void)?'

我有我創建了一個按鈕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

我在第三行收到錯誤(以alertController.addAction ):

無法將類型'(_,_)-> Void'的值轉換為預期的參數類型'((UIAlertAction)-> Void)?

我該如何解決?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM