哪些条件逻辑可用于Swift中的子类化布尔值?

时间:2018-07-19 15:36:18

标签: swift boolean uitextfield

我正在为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
        }

    }
}

1 个答案:

答案 0 :(得分:1)

您可以访问发件人

overlayButton.addTarget(self, action: #selector(showSecureCharacters(_:)), for: .touchUpInside)

//

@objc func showSecureCharacters(_ sender:UIButton){ --- }

这也足够

self.isSecureTextEntry = !self.isSecureTextEntry