ChatGPT解决这个技术问题 Extra ChatGPT

以编程方式快速更改 UIButton 的文本

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

currencySelector.text = "foobar"

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


M
Majster

在 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("购买", for: UIControlState.normal)
对于 swift 4 也 button.setTitle("Button Title",for: .normal) 工作!谢谢
s
shim

只是对 Swift 和 iOS 编程新手的澄清。下面的代码行:

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

仅适用于 IBOutlets,不适用于 IBActions

因此,如果您的应用程序使用按钮作为函数来执行某些代码,例如播放音乐,并且您想根据切换变量将标题从 Play 更改为 Pause,您还需要创建一个 {3 } 为那个按钮。

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


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

斯威夫特 5.0

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

D
Dmitry

斯威夫特 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 已被弃用
B
Bhavin Ramani

斯威夫特 3:

设置按钮标题:

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

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

C
Christian Navelot

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

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

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

根据 Apple SetTitle Doc

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

我有一个属性标题,我试图在它上面设置标题,但没有效果......


g
gustavoanalytics

斯威夫特 3

当您进行@IBAction 时:

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

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


m
midhun p

迅捷4.2及以上

使用按钮的 IBOutlet

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

使用按钮的 IBAction

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

y
yassine menssi

//对于正常状态:

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

M
Man of Progress

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

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

G
Giang

斯威夫特 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)

J
Jasani Hardik

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

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

}

并在您的按钮操作方法中调用此方法,例如;

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