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
到類。 有人可以幫忙嗎?
謝謝。
謝謝,你的代碼工作!我認爲在代碼上方定義「UITextFields」並單獨創建「UIAlertActions」是這裏的關鍵。 – Faruk
我還有一個問題。我如何創建'UIAlertController'像這樣:http://www.theappguruz.com/app/uploads/2015/06/screen21.png – Faruk
@Faruk UIActionSheet ui element –