How do I validate TextFields in an UIAlertController?

后端 未结 10 2297
不思量自难忘°
不思量自难忘° 2020-12-14 08:54

Can anyone tell me how to validate UITextFields inside of a UIAlertController?

I need it to prevent the user from clicking \"Save\" unless

10条回答
  •  一个人的身影
    2020-12-14 09:22

    This can be achieved via NSNotificationCenter before you display the alert controller, all you have to do is ask the notification center to observe the notification for UITextFieldTextDidChangeNotification and you should be good,

    Given below is the implementation for the same

    @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
    }
    

提交回复
热议问题