我用textField创建了一个AlertViewController,它要求用户为数据模型输入一个新项的名称。所以我想保持提交按钮禁用,而文本字段为空。在其他情况下,当我创建@IBOutlet在ViewController的文本字段从故事板我可以只使用一些方法从UITextFieldDelegate协议和解决问题,没有你的帮助。但现在不行了
textField
AlertViewController
@IBOutlet
ViewController
UITextFieldDelegate
uemypmqf1#
注册文本字段更改通知并验证其中的文本字段:
@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 }
a6b3iqyw2#
Swift 4.2更新
NotificationCenter.default.addObserver( forName: UITextField.textDidChangeNotification, object: alert.textFields?.first, queue: .main ) { notification in guard let textFieldText = alert.textFields?.first?.text else { return } saveAction.isEnabled = self.isValidEmail(textFieldText) && !textFieldText.isEmpty }
rfbsl7qr3#
在viewDidLoad方法中添加此代码
let notificationCenter = NSNotificationCenter.defaultCenter() notificationCenter.addObserver( self, selector: "textFieldTextChanged:", name:UITextFieldTextDidChangeNotification, object: nil )
ViewController中的此方法
func textFieldTextChanged(sender : AnyObject) { //your code }
3条答案
按热度按时间uemypmqf1#
注册文本字段更改通知并验证其中的文本字段:
a6b3iqyw2#
Swift 4.2更新
rfbsl7qr3#
在viewDidLoad方法中添加此代码
ViewController中的此方法