安全文本.echosbullets不适用于密码字段 [英] Secure text .echosbullets not working for password field
问题描述
这就是我所拥有的:
@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
}
}
除了密码字段中的文本不会在回显项目符号和回显真实文本之间更改状态之外,其他所有内容似乎都可以正常运行.一切也都正确地链接在一起-单元格位于文本字段中,密码按钮位于视图中,并且插座有效.我想知道这是否是"Mac上的Swift< iOS上的Swift"中的另一个.
这是最终的解决方案,如果有人愿意看到它的话:
@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
}
}
请注意,文本字段password
和visPswd
在视图中具有相同的大小和位置-始终保持隐藏状态以避免重叠.当用户在password
或visPswd
字段中输入值时,当复选框状态更改时,它将与另一个字段同步.
您可以完成您想要在安全字段顶部添加第二个文本字段的操作.将IBAction添加到您的复选框以切换字段isHidden属性,并复制另一个textField stringValue并将其设置为第一响应者.您的实现应如下所示:
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()
}
}
}
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.
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()
}
}
}
这篇关于安全文本.echosbullets不适用于密码字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!