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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26326296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:08:44  来源:igfitidea点击:

Changing text of UIButton programmatically swift

iosxcodeswiftuibutton

提问by rocket101

Simple question here. I have a UIButton, currencySelector, and I want to programmatically change the text. Here's what I have:

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

currencySelector.text = "foobar"

Xcode gives me the error "Expected Declaration". What am I doing wrong, and how can I make the button's text change?

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

回答by Gal Marom

In Swift 3, 4, 5:

在 Swift 3、4、5 中:

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

Otherwise:

除此以外:

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

Also an @IBOutlethas to declared for the button.

@IBOutlet必须为button.

回答by Bendrix

Just a clarification for those new to Swiftand iOSprogramming. Below line of code:

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

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

only applies to IBOutlets, not IBActions.

仅适用于IBOutlets,不适用于IBActions

So, if your app is using a button as a function to execute some code, say playing music, and you want to change the title from Playto Pausebased on a toggle variable, you need to also create an IBOutletfor that button.

所以,如果您的应用使用一个按钮的功能,以执行一些代码,说播放音乐,并希望标题从改变PlayPause基于一个切换变量,您还需要创建一个IBOutlet该按钮。

If you try to use button.setTitleagainst an IBActionyou will get an error. Its obvious once you know it, but for the noobs (we all were) this is a helpful tip.

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

回答by Dmitry

Swift 5:

斯威夫特 5:

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

回答by Bhavin Ramani

Swift 3:

斯威夫特 3:

Set button title:

设置按钮标题:

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

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

回答by Tyler Rutt

Swift 5.0

斯威夫特 5.0

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

回答by Christian Navelot

Changing title when attributed is a bit different :

归因时更改标题有点不同:

I just ran into a problem : If you have an UIButton with an Attributed Title, you have to use :

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

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

as, per Apple SetTitle Doc:

根据Apple SetTitle Doc

If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one.

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

I had an attributed title and I tried to setTitle on it, with no effect...

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

回答by gustavoanalytics

Swift 3

斯威夫特 3

When you make the @IBAction:

当你做@IBAction 时:

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

This sets the sender as UIButton (instead of Any) so it targets the btnAction as a UIButton

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

回答by midhun p

swift 4.2 and above

swift 4.2 及以上

using button's IBOutlet

使用按钮的 IBOutlet

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

using button's IBAction

使用按钮的 IBAction

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

回答by Jasani Hardik

To set a title for a button in Xcode using swift - 04: first create a method called setTitle with parameter title and UIController state like below ;

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

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

}

and recall this method in your button action method like ;

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

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

回答by Giang

Swift 3

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