Swift 中的子类布尔值可以使用什么条件逻辑?
What conditional logic can be used for subclassed booleans in Swift?
我正在对 UITextfield 进行子类化,以便在右侧创建一个覆盖按钮,该按钮将切换 isSecureTextEntry 布尔值。通常在故事板中创建 IBAction 时,布尔值是根据我在 Objective-C 代码中实现的发件人确定的,如下所示:
-(IBAction)showPasswordTapped:(UIButton *)sender{
sender.selected = !sender.selected;
self.passwordField.secureTextEntry = !sender.selected;
}
但是,既然我正在子类化,我正在考虑使用 if else 语句,但逻辑看起来好像只是将布尔值设置回 true。如果我无法访问发件人,合适的条件语句是什么?
这是我在 Swift 中的子类:
class SecurePasswordUITextField : UITextField {
override func awakeFromNib() {
super.awakeFromNib()
self.isSecureTextEntry = true
var overlayButton = UIButton.init(type: UIButtonType.custom)
overlayButton.titleLabel = "Show"
overlayButton.addTarget(self, action: #selector(showSecureCharacters), for: .touchUpInside)
overlayButton = CGRect.init(x: self.bounds.origin.x, y: self.bounds.origin.y, width: 28, height: 28)
self.rightView = overlayButton
self.rightViewMode = UITextFieldViewMode.always
}
func showSecureCharacters(){
if self.isSecureTextEntry {
self.isSecureTextEntry = false
} else {
self.isSecureTextEntry = true
}
if !self.isSecureTextEntry{
self.isSecureTextEntry = true
} else {
self.isSecureTextEntry = false
}
}
}
您可以访问发件人
overlayButton.addTarget(self, action: #selector(showSecureCharacters(_:)), for: .touchUpInside)
//
@objc func showSecureCharacters(_ sender:UIButton){ --- }
也够了
self.isSecureTextEntry = !self.isSecureTextEntry
我正在对 UITextfield 进行子类化,以便在右侧创建一个覆盖按钮,该按钮将切换 isSecureTextEntry 布尔值。通常在故事板中创建 IBAction 时,布尔值是根据我在 Objective-C 代码中实现的发件人确定的,如下所示:
-(IBAction)showPasswordTapped:(UIButton *)sender{
sender.selected = !sender.selected;
self.passwordField.secureTextEntry = !sender.selected;
}
但是,既然我正在子类化,我正在考虑使用 if else 语句,但逻辑看起来好像只是将布尔值设置回 true。如果我无法访问发件人,合适的条件语句是什么?
这是我在 Swift 中的子类:
class SecurePasswordUITextField : UITextField {
override func awakeFromNib() {
super.awakeFromNib()
self.isSecureTextEntry = true
var overlayButton = UIButton.init(type: UIButtonType.custom)
overlayButton.titleLabel = "Show"
overlayButton.addTarget(self, action: #selector(showSecureCharacters), for: .touchUpInside)
overlayButton = CGRect.init(x: self.bounds.origin.x, y: self.bounds.origin.y, width: 28, height: 28)
self.rightView = overlayButton
self.rightViewMode = UITextFieldViewMode.always
}
func showSecureCharacters(){
if self.isSecureTextEntry {
self.isSecureTextEntry = false
} else {
self.isSecureTextEntry = true
}
if !self.isSecureTextEntry{
self.isSecureTextEntry = true
} else {
self.isSecureTextEntry = false
}
}
}
您可以访问发件人
overlayButton.addTarget(self, action: #selector(showSecureCharacters(_:)), for: .touchUpInside)
//
@objc func showSecureCharacters(_ sender:UIButton){ --- }
也够了
self.isSecureTextEntry = !self.isSecureTextEntry