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.