【发布时间】:2017-10-22 23:31:04
【问题描述】:
这是我得到的:
@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
编辑:这是最终的解决方案,如果有人愿意看的话:
@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 字段中输入值时,它会在复选框状态更改时与另一个字段同步。
【问题讨论】:
标签: swift macos nstextfield