swift 文本字段为空时禁用UIAlertView中的按钮

23c0lvtd  于 2023-04-19  发布在  Swift
关注(0)|答案(3)|浏览(152)

我用textField创建了一个AlertViewController,它要求用户为数据模型输入一个新项的名称。
所以我想保持提交按钮禁用,而文本字段为空。在其他情况下,当我创建@IBOutletViewController的文本字段从故事板我可以只使用一些方法从UITextFieldDelegate协议和解决问题,没有你的帮助。
但现在不行了

uemypmqf

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

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

rfbsl7qr3#

在viewDidLoad方法中添加此代码

let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(
 self,
 selector: "textFieldTextChanged:",
 name:UITextFieldTextDidChangeNotification,
 object: nil
)

ViewController中的此方法

func textFieldTextChanged(sender : AnyObject) {
//your code
}

相关问题