2016-05-28 8 views
2

この関数の何が問題なのかよくわかりません。私は、ユーザーが選択した写真を削除したいかどうかを尋ねるアラートを表示しようとしています。非関数型の値を呼び出すことができませんUIAlertAction

写真を削除する機能がエラーを返す場合は、そのエラーをユーザーに表示します。

Xcodeは私が取る場合、私はスウィフト2

@IBAction func deletePhoto(sender: AnyObject) { 
    let alertController = UIAlertController(title: "Delete Photo", message: "Are you sure you want to delete this photo?", preferredStyle: .Alert) 
    let okAction = UIAlertAction(title: "OK", style: .Default) { UIAlertAction in 

     self.photoGateway!.delete(self.photo!.id!, completion: { (withError: Bool) -> Void in 

      if (withError == true) { 
       let errorController = UIAlertController(title: "Delete Failed", message: "blah", preferredStyle: UIAlertControllerStyle.Alert) 
       // The next line is causing the error 
       errorController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
       self.presentViewController(errorController, animated: true, completion: nil) 
      } else { 
       self.dismissViewControllerAnimated(true, completion: nil) 
      } 

     }) 

    } 

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { UIAlertAction in 
     print("Cancelled") 
    } 

    alertController.addAction(okAction) 
    alertController.addAction(cancelAction) 
    self.presentViewController(alertController, animated: true, completion: nil) 
} 

を使用してい

を「非機能型UIAlertActionの値を呼び出すことはできません」というメッセージでerrorController.addActionラインに失敗しています違反行がすべてうまくいけば、ユーザーはアラートを破棄する方法がありません

答えて

14

修正済みです。

errorController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 

に:変更...

errorController.addAction(UIKit.UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
+2

クール、それが動作する...しかし、なぜ私たちもこれを実行する必要がありますか?ある特定の場所を除いて、無数の場所で私のコード全体にスコーププレフィックスなしで動作します。 –

+1

この回答をありがとう。私はまた、なぜこれがハンドラの中で必要でないときにハンドラ内にあるという文脈において違いが生じるのか知りたいと思う。 –

関連する問題