【发布时间】:2015-12-03 15:18:02
【问题描述】:
我创建了一个UIAlertController,而不是在该控制器中创建了一个UITextFields。当我尝试验证输入的文本到文本字段时,它们返回 nil。这是代码:
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。
有人可以帮忙吗?
谢谢。
【问题讨论】:
标签: ios swift uitextfield uialertcontroller