And*_*rds 6 uitextfield ios swift ios12
在 iOS 12 中,我有一个用于注册流程的新密码文本字段,我希望系统建议一个强密码。我还有一个基于委托方法启用和禁用的按钮,我做了一些更改等。
textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String)
这对于在用户点击 时启用它很有用Use Strong Password。但是我似乎没有收到用户可能点击时的委托回调,Choose My Own Password因此我的按钮启用/禁用逻辑永远没有机会执行,允许某人使用空白密码进行注册。
关于当用户点击时我可能不需要做什么来获得回调的任何想法Choose my own password?任何帮助是极大的赞赏。
我不太熟悉“使用字符串密码”的委托,但如果用户选择自己的密码,您需要检查用户在密码文本字段中输入的内容并验证它是否符合您的标准。
为此,您需要使用其 target 检查密码 textField 中输入的内容.editingChanged。当用户键入其中的任何内容时,都会启用或禁用该按钮。查看步骤 #3 和步骤 #4。第 4 步将切换并决定启用或禁用按钮。
另一件需要注意的事情是,当 vc 第一次出现时,按钮应该是,disabled然后一旦密码文本字段数据有效,则启用按钮。步骤#1 和步骤#4
例如,在我的应用程序中,如果用户在密码文本字段内输入所有空格,则注册按钮将被禁用,但如果他们输入有效数据,则该按钮将被启用。
更新我在之后的步骤 3A 和 3B 中添加了viewDidLoad,因为正如 @DawsonToth 在评论中正确指出的那样,如果用户选择强密码,则将启用下一个按钮。但是,如果他们随后决定选择“选择我自己的密码”,则密码文本字段将被清除,但下一个按钮仍将启用。我没有考虑到这一点。
您需要KVO observer向passwordTextField 的“文本”添加 a keypath,以便每次passwordTextField 的文本更改时,都会调用handleTextInputChanged() ,从而在清除文本后禁用下一个按钮。
// 1. create a signUp button and make sure it is DISABLED and the color is .lightGray to signify that. In step 4 if the data inside the passwordTextField is valid I enable then button and change the color to blue
let signUpButton: UIButton = {
let button = UIButton(type: .system)
button.isEnabled = false // ** this must be set as false otherwise the user can always press the button **
button.setTitle("SignUp", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.backgroundColor = UIColor.lightGray
button.addTarget(self, action: #selector(signUpButtonTapped), for: .touchUpInside)
return button
}()
// 2. create the password textField and set its delegate in viewDidLoad. For eg self.passwordTextField.delegate = self
let passwordTextField: UITextField = {
let textField = UITextField()
textField.placeholder = "Enter Password"
textField.autocapitalizationType = .none
textField.returnKeyType = .done
textField.isSecureTextEntry = true
// 3. add the target for .editingChanged to check the textField
textField.addTarget(self, action: #selector(handleTextInputChanged), for: .editingChanged)
return textField
}()
override func viewDidLoad() {
super.viewDidLoad()
passwordTextField.delegate = self // if this is programmatic make sure to add UITextFieldDelegate after the class name
// 3A. Add a KVO observer to the passwordTextField's "text" keypath
passwordTextField.addObserver(self, forKeyPath: "text", options: [.old, .new], context: nil)
}
// 3B. call the observer
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "text" {
handleTextInputChanged()
}
}
// 4. this checks what's typed into the password textField from step 3
@objc fileprivate func handleTextInputChanged() {
let isFormValid = !isPasswordTextFieldIsEmpty() // if the textField ISN'T empty then the form is valid
if isFormValid {
signUpButton.isEnabled = true
signUpButton.backgroundColor = .blue
} else {
signUpButton.isEnabled = false
signUpButton.backgroundColor = .lightGray
}
}
// 5. create a function to check to see if the password textField is empty
func isPasswordTextFieldIsEmpty() -> Bool {
// 6. this checks for blank space
let whiteSpace = CharacterSet.whitespaces
// 7. if the passwordTextField has all blank spaces in it or is empty then return true
if passwordTextField.text!.trimmingCharacters(in: whitespace) == "" || passwordTextField.text!.isEmpty {
return true
}
return false // if it has valid characters in it then return false
}
// 8. target method from step 1
@objc func signUpButtonTapped() {
// run firebase code
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1895 次 |
| 最近记录: |