Secure text .echosbullets not working for password field

前端 未结 1 1144
轮回少年
轮回少年 2021-01-22 01:19

Here\'s what I\'ve got:

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


        
1条回答
  •  没有蜡笔的小新
    2021-01-22 02:18

    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

    0 讨论(0)
提交回复
热议问题