【问题标题】:change UIButton title after it was hidden隐藏后更改 UIButton 标题
【发布时间】:2017-07-02 15:26:36
【问题描述】:

我想做的是:

当用户点击“忘记密码”标签时: 1- UIButton 标题更改为“发送”

2-密码文本字段被隐藏

3 - 取消标签不隐藏

当用户点击取消时: 1- 显示密码文本字段

2- UIButton 标题为“登录”

3-舞蹈被隐藏

@IBAction func forgotTapped(_ sender: UITapGestureRecognizer) {

    if passwordtxt.isHidden == false {
        loginbtn.titleLabel?.text = "send"

    passwordtxt.isHidden = true
        forgotLab.isHidden = true
        top.constant = -50

        cancelLb.isHidden = false
    } else {
         if passwordtxt.isHidden == true  {
        forgotLab.isHidden = false

        if passwordtxt.isHidden == true {

            passwordtxt.isHidden = false
               loginbtn.titleLabel?.text = "login"
            cancelLb.isHidden = true
        }
    }

    }

}

@IBAction func cancelTapped(_ sender: Any) {

    cancelLb.isHidden = true
    forgotLab.isHidden = false
    loginbtn.titleLabel?.text = "send"

    if passwordtxt.isHidden == true {

        passwordtxt.isHidden = false

        top.constant = 50


}
}

问题:

现在一切正常,但是当我单击“发送”按钮时,它会变为“登录”!它会回到它的默认黑色标题

有没有办法让 UIButton 在点击后仍保持“发送”状态?

更新

  if passwordtxt.isHidden == false {
        loginbtn.setTitle("send", for: .normal)

    passwordtxt.isHidden = true
        forgotLab.isHidden = true
        top.constant = -50

        cancelLb.isHidden = false
    } else {
         if passwordtxt.isHidden == true  {
        forgotLab.isHidden = false

        if passwordtxt.isHidden == true {

            passwordtxt.isHidden = false
            loginbtn.setTitle("login", for: .normal)
            cancelLb.isHidden = true
        }
    }

    }

}

@IBAction func cancelTapped(_ sender: Any) {

    cancelLb.isHidden = true
    forgotLab.isHidden = false
    loginbtn.setTitle("send", for: .normal)

    if passwordtxt.isHidden == true {

        passwordtxt.isHidden = false

        top.constant = 50
}
}

【问题讨论】:

  • 有,但首先,当点击“发送”UIButton 时,您希望看到什么确切? (执行此操作的“蛮力”方法是让 forgotTapped(sender:) 中的逻辑检查标签中的内容并进行相应处理。但可能有更优雅的方式。)
  • 更改标题时使用函数setTitle 而不是从titleLabel 更改它。
  • 我想成为“送”
  • 你是什么意思? @TawaNicolas
  • 而不是使用loginbtn.titleLabel?.text = "send" 做loginbtn.setTitle("send", for: .normal)

标签: ios swift xcode uibutton


【解决方案1】:
@IBAction func forgotTapped(_ sender: UITapGestureRecognizer) {

    if passwordtxt.isHidden == false {

    passwordtxt.isHidden = true
        loginbtn.setTitle("send", for: .normal)

        forgotLab.isHidden = true
        top.constant = -50

        cancelLb.isHidden = false
    } else {
         if passwordtxt.isHidden == true  {

        forgotLab.isHidden = false

        if passwordtxt.isHidden == true {

            passwordtxt.isHidden = false
            cancelLb.isHidden = true
        }
    }

    }

}

@IBAction func cancelTapped(_ sender: Any) {
    loginbtn.setTitle("login", for: .normal)

    cancelLb.isHidden = true
    forgotLab.isHidden = false

    if passwordtxt.isHidden == true {

        passwordtxt.isHidden = false

        top.constant = 50


}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 2022-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多