文本字段为空时禁用 UIAlertView 中的按钮
Disable button in UIAlertView while text field is empty
我用 textField
创建了一个 AlertViewController
,要求用户为数据模型键入新项目的名称。
所以我想在文本字段为空时禁用提交按钮。在其他情况下,当我在 ViewController
中为情节提要中的文本字段创建 @IBOutlet
时,我可以只使用 UITextFieldDelegate
协议中的一些方法并在没有你帮助的情况下解决问题。
但现在我不能。
注册文本字段更改通知并在那里验证文本字段:
@IBAction func showAlert(sender: AnyObject) {
var alert = UIAlertController(title: "New user",
message: "Add a new user",
preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save",
style: .Default) { (action: UIAlertAction!) -> Void in
println("do your stuff here")
}
saveAction.enabled = false
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction!) -> Void in
}
alert.addTextFieldWithConfigurationHandler {
(textFieldName: UITextField!) in
textFieldName.placeholder = "Enter full name"
}
alert.addTextFieldWithConfigurationHandler {
(textFieldEmail: UITextField!) in
textFieldEmail.placeholder = "Enter valid email adress"
textFieldEmail.keyboardType = .EmailAddress
}
// adding the notification observer here
NSNotificationCenter.defaultCenter().addObserverForName(UITextFieldTextDidChangeNotification, object:alert.textFields?[0],
queue: NSOperationQueue.mainQueue()) { (notification) -> Void in
let textFieldName = alert.textFields?[0] as! UITextField
let textFieldEmail = alert.textFields![1] as! UITextField
saveAction.enabled = self.isValidEmail(textFieldEmail.text) && !textFieldName.text.isEmpty
}
NSNotificationCenter.defaultCenter().addObserverForName(UITextFieldTextDidChangeNotification, object:alert.textFields?[1],
queue: NSOperationQueue.mainQueue()) { (notification) -> Void in
let textFieldEmail = alert.textFields?[1] as! UITextField
let textFieldName = alert.textFields?[0] as! UITextField
saveAction.enabled = self.isValidEmail(textFieldEmail.text) && !textFieldName.text.isEmpty
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert,
animated: true,
completion: nil)
}
// email validation code method
func isValidEmail(testStr:String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}"
if let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) as NSPredicate? {
return emailTest.evaluateWithObject(testStr)
}
return false
}
在 viewDidLoad 方法中添加这段代码
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(
self,
selector: "textFieldTextChanged:",
name:UITextFieldTextDidChangeNotification,
object: nil
)
和ViewController
中的这个方法
func textFieldTextChanged(sender : AnyObject) {
//your code
}
新 Swift 4.2 更新
NotificationCenter.default.addObserver(
forName: UITextField.textDidChangeNotification,
object: alert.textFields?.first,
queue: .main) { (notification) -> Void in
guard let textFieldText = alert.textFields?.first?.text else { return }
saveAction.enabled = self.isValidEmail(textFieldText) && !textFieldText.isEmpty
}
我用 textField
创建了一个 AlertViewController
,要求用户为数据模型键入新项目的名称。
所以我想在文本字段为空时禁用提交按钮。在其他情况下,当我在 ViewController
中为情节提要中的文本字段创建 @IBOutlet
时,我可以只使用 UITextFieldDelegate
协议中的一些方法并在没有你帮助的情况下解决问题。
但现在我不能。
注册文本字段更改通知并在那里验证文本字段:
@IBAction func showAlert(sender: AnyObject) {
var alert = UIAlertController(title: "New user",
message: "Add a new user",
preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save",
style: .Default) { (action: UIAlertAction!) -> Void in
println("do your stuff here")
}
saveAction.enabled = false
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction!) -> Void in
}
alert.addTextFieldWithConfigurationHandler {
(textFieldName: UITextField!) in
textFieldName.placeholder = "Enter full name"
}
alert.addTextFieldWithConfigurationHandler {
(textFieldEmail: UITextField!) in
textFieldEmail.placeholder = "Enter valid email adress"
textFieldEmail.keyboardType = .EmailAddress
}
// adding the notification observer here
NSNotificationCenter.defaultCenter().addObserverForName(UITextFieldTextDidChangeNotification, object:alert.textFields?[0],
queue: NSOperationQueue.mainQueue()) { (notification) -> Void in
let textFieldName = alert.textFields?[0] as! UITextField
let textFieldEmail = alert.textFields![1] as! UITextField
saveAction.enabled = self.isValidEmail(textFieldEmail.text) && !textFieldName.text.isEmpty
}
NSNotificationCenter.defaultCenter().addObserverForName(UITextFieldTextDidChangeNotification, object:alert.textFields?[1],
queue: NSOperationQueue.mainQueue()) { (notification) -> Void in
let textFieldEmail = alert.textFields?[1] as! UITextField
let textFieldName = alert.textFields?[0] as! UITextField
saveAction.enabled = self.isValidEmail(textFieldEmail.text) && !textFieldName.text.isEmpty
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert,
animated: true,
completion: nil)
}
// email validation code method
func isValidEmail(testStr:String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}"
if let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) as NSPredicate? {
return emailTest.evaluateWithObject(testStr)
}
return false
}
在 viewDidLoad 方法中添加这段代码
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(
self,
selector: "textFieldTextChanged:",
name:UITextFieldTextDidChangeNotification,
object: nil
)
和ViewController
中的这个方法func textFieldTextChanged(sender : AnyObject) {
//your code
}
新 Swift 4.2 更新
NotificationCenter.default.addObserver(
forName: UITextField.textDidChangeNotification,
object: alert.textFields?.first,
queue: .main) { (notification) -> Void in
guard let textFieldText = alert.textFields?.first?.text else { return }
saveAction.enabled = self.isValidEmail(textFieldText) && !textFieldText.isEmpty
}