【问题标题】:SWIFT: Button Title changing back to original title unwarrantedSWIFT:按钮标题更改回原来的标题是没有根据的
【发布时间】:2021-02-24 22:25:47
【问题描述】:

所以我有以下代码在点击我的分段控件的一侧或另一侧时执行:

@IBAction func logInOrSignUpIndexChanged(_ sender: UISegmentedControl) {
    
    // Correct setup for Sign Up
    if sender.selectedSegmentIndex == 0 {
        
        confirmPasswordTextfield.isHidden = false
        confirmPasswordTextfield.isUserInteractionEnabled = true
        createAccountOrLogInButton.titleLabel?.text = "Create Account"
        rememberMeViewTopConstraint.constant = 20
        
    } // Correct setup for Log In
    else if sender.selectedSegmentIndex == 1 {
        
        confirmPasswordTextfield.isHidden = true
        confirmPasswordTextfield.isUserInteractionEnabled = false
        createAccountOrLogInButton.titleLabel?.text = "Log In"
        rememberMeViewTopConstraint.constant = -34
        
    }
    
}

由于某种原因,当我按下分段索引值 1(所以底部的 if 语句执行)时,按钮标题会在一瞬间变为“登录”,然后恢复为“创建帐户”。我尝试更改当然不起作用的按钮的初始标题,并尝试删除第一个 if 语句中的行,将标题更改为“创建帐户”,并且标题仍然更改,所以我知道首先,如果出于某种任意原因没有再次执行 if 语句。知道有什么问题吗?

【问题讨论】:

    标签: ios swift xcode uibutton title


    【解决方案1】:

    改变

    createAccountOrLogInButton.titleLabel?.text = "Log In"
    

    createAccountOrLogInButton.setTitle("Log In",for:.normal)
    

    第一个if也一样

    【讨论】:

    • 这行得通,我会在 SO 允许的几分钟内接受它。不过我还有一个问题。当我这样做时,为什么标题会以淡入淡出的方式发生变化?换句话说,当我切换段时,它会淡出当前标题,然后淡入新标题。我没有故意将其设置为那样。不过感谢您的帮助
    • 这正是 UIKit 所做的。您可以将setTitle 放在performWithoutAnimation 中以消除效果
    • 我在整个视图控制器中都在输入 performWithoutAnimation 并且没有弹出任何功能......我做错了什么吗?不知道如何应用这个。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2019-11-15
    • 2013-09-01
    • 1970-01-01
    • 2023-03-19
    • 2019-03-29
    相关资源
    最近更新 更多