2016-04-23 21 views
0

私は、にボタンを作成しました。このボタンは、deleteButtonという名前(var)を持っています。このボタンは各セルにあります。エラー: '(_、_) - > Void'の値を期待される引数の型に変換できません '((UIAlertAction) - > Void)?'

ユーザーがボタンを押すと、警告がポップアップして削除を確認します。ここにコードがあります。私が言って(alertController.addActionで始まる)3行目でエラーが発生します

cell.deleteButton.tag = indexPath.row 

@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:方法では、私はこの行を宣言した

Cannot convert value of type '(_, _) -> Void' to expected argument type '((UIAlertAction) -> Void)?'

どのように操作を行いますこれを修正しますか?

答えて

3

ハンドラのパラメータUIAlertActionコンストラクタは、選択したアクションオブジェクトを唯一のパラメータとして取ります。

の代わりに、このエラーが、その後行くこと

alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction) -> Void in 
+0

、と私は '**値を変換できません'(sender.tag)上のエラーを言ってます:

alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (action, indexPath) -> Void in 

あなたがこれをお勧めします型 'Int'から期待される引数型 'NSIndexPath' **に変更します。 –

+0

objectAtIndexPathに渡すNSIndexPathオブジェクトを作成する必要があります。 indexPath = NSIndexPath(forRow:sender.tag、inSection:0)にしてから、indexpathをobjectAtIndexPath(indexPath)に設定してください。 – Sebastian

+0

パーフェクト!これは動作します:)) –

関連する問題