2016-09-01 12 views
0

とアラート私は拡張子を使用してテキストフィールドにuialertcontrollerを使用して関数を作成しようとしているIOSスウィフト2:拡張機能 - テキストフィールド

uialertcontrollerが、これは私のコードです:

extension UIAlertController{ 

class func AlertWithTextField(here: String, message1 : String) -> UIAlertController{ 

    var alertController:UIAlertController? 
    alertController = UIAlertController(title: here, 
     message: message1, 
     preferredStyle: .Alert) 

    alertController!.addTextFieldWithConfigurationHandler(
     {(textField: UITextField!) in 
      textField.placeholder = "Ex: 1" 
      textField.textAlignment = .Center 
      textField.delegate = self 
      textField.keyboardType = UIKeyboardType.NumberPad 
    }) 
    let action = UIAlertAction(title: "Submit", 
     style: UIAlertActionStyle.Default, 
     handler: {[weak self] 
      (paramAction:UIAlertAction!) in 
      if let textFields = alertController?.textFields{ 
       let theTextFields = textFields as! [UITextField] 
       let enteredText = theTextFields[0].text 
       print("\n\(enteredText)") } 
     }) 
    let action2 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) 
    alertController?.addAction(action) 
    alertController?.addAction(action2) 

}} 

大丈夫、私は問題を抱えています言葉 "自己"と私は彼らのための解決策を見つけることができません、この問題の解決策は何ですか?

あなたの最初の自己の問題については
+0

私は既にこの質問をしました。ここで見つけることができます:http://stackoverflow.com/questions/26567413/get-input-value-from-textfield-in-ios-alert-in-swift :) – ntoonio

答えて

0

、私はあなたがあなたの静的メソッドでUITextFieldDelegateオブジェクトを受け入れ、それがデリゲートをTI割り当て、あなたの第二の問題のために、あなたが弱い宣言されていることができ、この

class func AlertWithTextField(here: String, message1 : String, delegate:UITextFieldDelegate?) -> UIAlertController{ 

    var alertController:UIAlertController? 
    alertController = UIAlertController(title: here, 
             message: message1, 
             preferredStyle: .Alert) 

    alertController!.addTextFieldWithConfigurationHandler(
     {(textField: UITextField!) in 
      textField.placeholder = "Ex: 1" 
      textField.textAlignment = .Center 
      textField.delegate = delegate 
      textField.keyboardType = UIKeyboardType.NumberPad 
    }) 
    let action = UIAlertAction(title: "Submit", 
           style: UIAlertActionStyle.Default, 
           handler: {(paramAction:UIAlertAction!)->Void in 
           if let textFields = alertController?.textFields { 
            let theTextFields = textFields 
            let enteredText = theTextFields[0].text 
            print("\n\(enteredText)") } 
     }) 
    let action2 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) 
    alertController?.addAction(action) 
    alertController?.addAction(action2) 
    return alertController! 
} 

ような何かを行うことをお勧め自己は閉鎖でそれを使用していないので、それを削除すればコードはうまく動作するはずです。