Secure text .echosbullets not working for password field

China☆狼群 提交于 2019-12-04 05:11:59

问题


Here's what I've got:

@IBOutlet weak var password: NSSecureTextField! 
@IBOutlet weak var shwpswd: NSButton! //Checkbox
@IBOutlet weak var pswdcell: NSSecureTextFieldCell! //Cell


@IBAction func shwpswd(_ sender: Any) {
    if(shwpswd.state == 1) {
        pswdcell.echosBullets = false // Turn the Secure text into regular text
    }
    else if(shwpswd.state == 0) {
        pswdcell.echosBullets = true // Secure text
    }
}

Everything seems to run fine, except the text in the password field doesn't change states between echoing bullets and echoing the real text. Everything is linked together properly too - Cell is within the text field, password button is in the view and the outlet works. I'm wondering if this is another one of the "Swift on mac < Swift on iOS cases".

EDIT: Here is the final solution, should anyone care to see it:

@IBOutlet weak var shwpswd: NSButton! //Checkbox
@IBOutlet weak var visPswd: NSTextfield! //hidden regular box to show chars
@IBOutlet weak var password: NSSecureTextField!    //visible initial secure box
@IBAction func shwpswd(_ sender: Any) {
    if(shwpswd.state == 1) {
        self.visPswd.stringValue = self.password.stringValue //Sync both the text fields
        self.password.isHidden = true //hide the secure field
        self.visPswd.isHidden = false //show the real character echo field
    }
    else if(shwpswd.state == 0) {
        self.password.stringValue = self.visPswd.stringValue //Sync the two
        self.password.isHidden = false // Inverse of above
        self.visPswd.isHidden = true
    }
}

Note the text fields password and visPswd are the same size and position in the view - one remains hidden at all times to avoid overlapping. When the user enters values in either the password or visPswd field, it syncs with the other field when the checkbox state is changed.


回答1:


You can accomplish what you want adding a second text field in top of your secure field. Add an IBAction to your check box to switch your fields isHidden property and copy the other textField stringValue and make it the first responder. Your implementation should look like something like this:

import Cocoa

class ViewController: NSViewController {
    @IBOutlet weak var password: NSSecureTextField!
    @IBOutlet weak var showPassword: NSTextField!
    @IBOutlet weak var shwpswd: NSButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        shwpswd.state = .off
        showPassword.isHidden = true
    }
    override func viewDidAppear() {
        super.viewDidAppear()
        password.window?.makeFirstResponder(password)
    }
    @IBAction func showHidePassword(_ sender: NSButton) {
        showPassword.isHidden = !showPassword.isHidden
        password.isHidden = !password.isHidden
        if !showPassword.isHidden {
            showPassword.stringValue = password.stringValue
            showPassword.becomeFirstResponder()
        } else {
            password.stringValue = showPassword.stringValue
            password.becomeFirstResponder()
        }
    }
}

show/hide password sample



来源:https://stackoverflow.com/questions/46880160/secure-text-echosbullets-not-working-for-password-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!