iOS的12建议强密码文本字段委托回调选择我自己的密码

问题描述 投票:3回答:2

在iOS中12我有一个注册流程一个新的密码文本框,我想系统提出强烈的一个。我也有一个按钮,允许和禁止基于委托方法,我做了一些改变等

textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String)

这工作好当用户点击Use Strong Password启用它。但我似乎并没有得到时,用户可能点击Choose My Own Password委托回调和A S结果我的按钮启用/禁用逻辑从来没有得到执行的机会,让别人使用空白密码注册。

Suggested password

什么我可能会做T当用户点击Choose my own password得到一个回调任何想法?任何帮助是极大的赞赏。

ios swift uitextfield ios12
2个回答
1
投票

我面临着同样的问题,但是我发现在提供回调的唯一的事情就是viewDidLayoutSubviewsUIView代表

因此,我所做的就是使用这样的:

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if passwordTextField.isFirstResponder {
           validatePassword(passwordTextField.text ?? "")
        }
    }

0
投票

我不熟悉的委托“使用字符串密码”,但如果用户选择自己的密码,您需要检查什么类型的用户在密码文本框,并确认符合您的条件。

要做到这一点,你需要使用它的目标.editingChanged检查什么键入密码文本框。由于这里面无论去将启用或禁用此按钮,用户类型。再看步骤#3和步骤#4。第4步就是将切换,并决定枯萎启用或禁用按钮。

采取的注意的另一件事是,VC第一次出现时,按钮应disabled再一次密码文本框的数据是有效的,则启用按钮,然后。步骤#1和步骤#4

因为如果用户在密码文本框的注册按钮将被禁用里面的所有空格进入,但如果他们在有效数据输入的按钮将被激活例子在我的应用程序。

更新我在步骤3A和3B加入viewDidLoad因为事后@DawsonToth正确地指出了意见,如果用户选择一个强密码下一步按钮将被启用。但是,如果他们再决定挑选选择自己的密码的passwordTextField将清除,但Next按钮,将仍然被启用。我没有考虑这一点。

你需要一个KVO observer添加到passwordTextField的“文本” 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
}
© www.soinside.com 2019 - 2024. All rights reserved.