2015-12-03 44 views
1

我創建了一個UIAlertController而不是在該控制器中創建了UITextFields。當我嘗試驗證輸入的文本到文本字段時,它們返回nil。下面是代碼:如何檢查在UIAlertController中創建的UITextField的文本?

let loginWithMailAlert = UIAlertController(title: "Log In with Your Mail Address", message: "Please Enter Your E-Mail Address and Your Password", preferredStyle: UIAlertControllerStyle.Alert) 
     loginWithMailAlert.addTextFieldWithConfigurationHandler({[weak self](usernameField: UITextField!) in 
      usernameField.delegate = self 
      usernameField.placeholder = "E-Mail Address" 
     }) 
     loginWithMailAlert.addTextFieldWithConfigurationHandler({[weak self](passwordField: UITextField!) in 
      passwordField.delegate = self 
      passwordField.placeholder = "Password" 
      passwordField.secureTextEntry = true 
     }) 
     loginWithMailAlert.addAction(UIAlertAction(title: "Log In", style: UIAlertActionStyle.Default, handler:{ 
      (loginWithMailAlert: UIAlertAction!) in 
      print("Pressed 'Log In'") 
      if self.usernameField.text == "faruk" && self.passwordField.text == "123" { 
       self.performSegueWithIdentifier("loginToMain", sender: nil) 
      }else{ 
       print("Error") 
       print("Username::::\(self.usernameField.text as String!)") 
      } 
     })) 
     loginWithMailAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler:{ 
      (loginWithMailAlert: UIAlertAction!) in 
      print("Pressed 'Cancel'") 

     })) 

     self.presentViewController(loginWithMailAlert, animated: true, completion: nil) 

這裏是輸出:

Pressed 'Log In' 
Error 
Username:::: 

我加UITextFieldDelegate到類。 有人可以幫忙嗎?

謝謝。

回答

1

嗯解決你的問題只是使用我的小代碼snipet。在確定按鈕,你可以簡單地檢查字段的值:

 var loginTextField: UITextField? 
     var passwordTextField: UITextField? 

     let alertController = UIAlertController(title: "UIAlertController", message: "UIAlertController With TextField", preferredStyle: .Alert) 
     let ok = UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in 
      //Check string hear !! 
      println(loginTextField?.text) 
     }) 
     let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (action) -> Void in 
      println("Cancel Button Pressed") 
     } 
     alertController.addAction(ok) 
     alertController.addAction(cancel) 
     alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in 
      // Enter the textfiled customization code here. 
      loginTextField = textField 
      loginTextField?.placeholder = "User ID" 
     } 
     alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in 
      // Enter the textfiled customization code here. 
      passwordTextField = textField 
      passwordTextField?.placeholder = "Password" 
      passwordTextField?.secureTextEntry = true 
     } 

     presentViewController(alertController, animated: true, completion: nil) 
+0

謝謝,你的代碼工作!我認爲在代碼上方定義「UITextFields」並單獨創建「UIAlertActions」是這裏的關鍵。 – Faruk

+0

我還有一個問題。我如何創建'UIAlertController'像這樣:http://www.theappguruz.com/app/uploads/2015/06/screen21.png – Faruk

+1

@Faruk UIActionSheet ui element –

相關問題