【问题标题】:Changing text of UIButton programmatically swift以编程方式快速更改 UIButton 的文本
【发布时间】:2014-12-07 05:01:57
【问题描述】:

这里有一个简单的问题。我有一个 UIButton、currencySelector,我想以编程方式更改文本。这是我所拥有的:

currencySelector.text = "foobar"

Xcode 给我错误“预期声明”。我做错了什么,如何更改按钮的文本?

【问题讨论】:

    标签: ios xcode swift uibutton


    【解决方案1】:

    在 Swift 3、4、5 中:

    button.setTitle("Button Title", for: .normal)
    

    否则:

    button.setTitle("Button Title", forState: UIControlState.Normal)
    

    还必须为button 声明一个@IBOutlet

    【讨论】:

    • 你可以省略UIControlState。例如forState: .Normal
    • Swift3 现在是.normal 注意小写
    • Swift3 将 forState 更改为 for
    • Swift3: self.btnBuy.setTitle("Buy", for: UIControlState.normal)
    • 对于 swift 4 也 button.setTitle("Button Title",for: .normal) 工作!谢谢
    【解决方案2】:

    只是对 SwiftiOS 编程新手的说明。下面的代码行:

    button.setTitle("myTitle", forState: UIControlState.Normal)
    

    仅适用于IBOutlets,不适用于IBActions

    因此,如果您的应用程序使用按钮作为函数来执行某些代码,例如播放音乐,并且您希望基于切换变量将标题从 Play 更改为 Pause,您还需要创建该按钮的IBOutlet

    如果您尝试对IBAction 使用button.setTitle,您将收到错误消息。一旦你知道它就很明显,但对于新手(我们都是)这是一个有用的提示。

    【讨论】:

    • 是的,在看到您的回答之前,我正准备用谷歌搜索这个。谢谢! +1
    • 这是IOS中的一个模式,我花了一段时间才发现。除非创建 IBOutlet,否则您无法访问 UI 项的某些属性。如果您尝试更改 UI 的属性但无法访问它,请确保您拥有运行某些代码的 IBAction 和提供对属性的访问的 IBOutlet。
    • 这个答案没有意义。动作的sender 将是按钮。您可以将任何您希望的内容应用到sender。你不需要一个出口来做到这一点。
    • 本迪克斯的回答和他的评论都不正确。他们完全错了。
    • 问题没有提到界面生成器,所以错误地假设是这种情况。
    【解决方案3】:

    斯威夫特 5.0

    // Standard State
    myButton.setTitle("Title", for: .normal)
    

    【讨论】:

      【解决方案4】:

      斯威夫特 5:

          let controlStates: Array<UIControl.State> = [.normal, .highlighted, .disabled, .selected, .focused, .application, .reserved]
          for controlState in controlStates {
              button.setTitle(NSLocalizedString("Title", comment: ""), for: controlState)
          }
      

      【讨论】:

      • 我不得不使用 UIControl.State。它说 UIControlState 已被弃用
      【解决方案5】:

      斯威夫特 3:

      设置按钮标题:

      //for normal state:
      my_btn.setTitle("Button Title", for: .normal)
      
      // For highlighted state:
      my_btn.setTitle("Button Title2", for: .highlighted)
      

      【讨论】:

        【解决方案6】:

        属性时更改标题有点不同:

        我刚刚遇到了一个问题:如果你有一个带有属性标题的 UIButton,你必须使用:

        my_btn.setAttributedTitle(NSAttributedString(string: my_title), for: my_state)
        

        根据Apple SetTitle Doc

        如果您同时为按钮设置了标题和属性标题,则按钮更喜欢使用属性标题而不是这个。

        我有一个属性标题,我尝试在其上设置标题,但没有任何效果...

        【讨论】:

          【解决方案7】:

          斯威夫特 3

          当您进行@IBAction 时:

          @IBAction func btnAction(_ sender: UIButton) {
            sender.setTitle("string goes here", for: .normal)
          }
          

          这会将发送者设置为 UIButton(而不是 Any),因此它将 btnAction 定位为 UIButton

          【讨论】:

            【解决方案8】:

            swift 4.2 及以上版本

            使用按钮的 IBOutlet

            btnOutlet.setTitle("New Title", for: .normal)
            

            使用按钮的 IBAction

            @IBAction func btnAction(_ sender: UIButton) {
              sender.setTitle("New Title", for: .normal)
            }
            

            【讨论】:

              【解决方案9】:

              //正常状态:

              btnSecurite.setTitle("TextHear", for: .normal)
              

              【讨论】:

                【解决方案10】:

                斯威夫特 3

                let button: UIButton = UIButton()
                button.frame = CGRect.init(x: view.frame.width/2, y: view.frame.height/2, width: 100, height: 100)
                button.setTitle(“Title Button”, for: .normal)
                

                【讨论】:

                  【解决方案11】:

                  在 Xcode 中使用 swift - 04 为按钮设置标题: 首先创建一个名为 setTitle 的方法,其参数为 title 和 UIController 状态,如下所示;

                  func setTitle(_ title : String?, for state : UIControl.State)   {
                  
                  }
                  

                  并在您的按钮操作方法中调用此方法 喜欢;

                  yourButtonName.setTitle("String", for: .state)
                  

                  【讨论】:

                    【解决方案12】:

                    截至 2021 年 12 月 12 日 - Swift 版本 5.5.1^ 假设您已经有一个正常状态下链接到 yourButton 的 IBOutlet。

                    yourButton.setTitle("Title of your button", for: .normal)
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2016-07-27
                      • 1970-01-01
                      • 1970-01-01
                      • 2015-09-29
                      • 2011-12-27
                      • 1970-01-01
                      • 2016-01-12
                      • 2015-05-12
                      相关资源
                      最近更新 更多