ios 如何在 Swift Xcode 6 中更改按钮文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26641571/
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:25:11  来源:igfitidea点击:

How to change button text in Swift Xcode 6?

iosswift

提问by GuavaMantis

Here's what I'm trying to do. If you've ever played Halo or CoD, you'd know that you could change the name of a weapon load-out.

这就是我想要做的。如果您曾经玩过 Halo 或 CoD,您就会知道您可以更改武器装载的名称。

What I'm doing is making it so you can change your load-out name using a text field. Here's the problem, the load-out name in the load-out menu is a button (to select and view info about that load-out) and I could just write this:

我正在做的是制作它,以便您可以使用文本字段更改加载名称。问题是,加载菜单中的加载名称是一个按钮(用于选择和查看有关该加载的信息),我可以这样写:

@IBAction func renameClassButton(sender: AnyObject) {
    classTopButton.text = "\(classTopTextField)"
}

Except it [classTopButton] is a button which doesn't allow the '.text' suffix

除了它 [classTopButton] 是一个不允许 '.text' 后缀的按钮

回答by Steve Rosenberg

You can do:

你可以做:

button.setTitle("my text here", forState: .normal)

Swift 3 and 4:

斯威夫特 3 和 4:

button.setTitle("my text here", for: .normal)

回答by Nikola Lukic

In Xcode 8 - Swift 3:

在 Xcode 8 - Swift 3 中

button.setTitle( "entertext" , for: .normal )

button.setTitle( "entertext" , for: .normal )

回答by Ty Victorson

It is now this For swift 3,

现在是这样 对于 swift 3,

    let button = (sender as AnyObject)
    button.setTitle("Your text", for: .normal)

(The constant declaration of the variable is not necessary just make sure you use the sender from the button like this) :

(变量的常量声明不是必需的,只需确保您像这样使用按钮中的发送者):

    (sender as AnyObject).setTitle("Your text", for: .normal)

Remember this is used inside the IBAction of your button.

请记住,这是在按钮的 IBAction 中使用的。

回答by Giacomo Sighinolfi

swift 4 work as well as 3

swift 4 工作以及 3

libero.setTitle("---", for: .normal)

where libero is a uibutton

libero 是一个 uibutton

回答by mzonerz

You can Use sender argument

您可以使用发件人参数

  @IBAction func TickToeButtonClick(sender: AnyObject) {

        sender.setTitle("my text here", forState: .normal)


}

回答by Nathan F.

Note that if you're using NSButton there is no setTitlefunc, instead, it's a property.

请注意,如果您使用的是 NSButton,则没有setTitlefunc,而是一个属性。

@IBOutlet weak var classToButton: NSButton!

. . .


classToButton.title = "Some Text"

回答by Grzegorz R. Kulesza

In Swift 4 I tried all of this previously, but runs only:

在 Swift 4 中,我之前尝试了所有这些,但仅运行:

@IBAction func myButton(sender: AnyObject) {
   sender.setTitle("This is example text one", for:[])
   sender.setTitle("This is example text two", for: .normal)
}

回答by giacoder

NOTE:

笔记:

line

线

someButton.setTitle("New Title", forState: .normal)

works only when Title type is Plain.

仅当 Title 类型为Plain时才有效。

enter image description here

在此处输入图片说明