ios 单击按钮时如何更改按钮的文本?- 斯威夫特
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28468906/
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
How do I change the text of a button when it is clicked? - Swift
提问by Jaxon
I'm new to programming so keep that in mind. I have looked at several other questions on this site, and tried the answers they gave, but those haven't worked for me. The code I am trying to use to change the button to is this:
我是编程新手,所以请记住这一点。我在这个网站上查看了其他几个问题,并尝试了他们给出的答案,但这些对我不起作用。我试图用来将按钮更改为的代码是:
@IBAction func AnswerButtonA(sender: UIButton)
{
[AnswerButtonA setTitle:@"Hello World" forState:UIControlStateNormal];
}
This is the most common method to do this I have found but it brings up several errors "Expected declaration" "Expected attribute name" "expected ',' separator, and a few others.
这是我发现的最常用的方法,但它带来了几个错误“预期声明”“预期属性名称”“预期','分隔符,以及其他一些错误。
Another thing I tried that others suggested is:
我尝试过其他人建议的另一件事是:
@IBAction func AnswerButtonA(sender: UIButton)
{
AnswerButtonA.setTitle("Hello World")
}
This as well as a few other things I've tried all bring up the error "'(UIButton) ->()' does not have a member named 'setTitle'.
这以及我尝试过的其他一些事情都带来了错误“'(UIButton)->()'没有名为'setTitle'的成员。
I realize this may be a stupid question but I can't figure it out. This is the first app I have tried to develop on my own and I have done fine until this problem. Any help would be appreciated, and if you could go into detail about what I'm doing wrong that would be amazing!
我意识到这可能是一个愚蠢的问题,但我无法弄清楚。这是我尝试自己开发的第一个应用程序,并且在出现此问题之前我做得很好。任何帮助将不胜感激,如果您能详细说明我做错了什么,那就太棒了!
(I only want the button to change when it is pressed, not when the view is loaded.)
(我只希望按钮在按下时发生变化,而不是在视图加载时发生变化。)
回答by Aaron Brager
In your first question you're trying to use Objective-C code in a Swift file.
在您的第一个问题中,您尝试在 Swift 文件中使用 Objective-C 代码。
In your second example, you're not passing the control state.
在您的第二个示例中,您没有传递控制状态。
In both instances, as Jeremy Pope points out, you're referring to AnswerButtonA
as the method, not the button.
在这两种情况下,正如 Jeremy Pope 所指出的,您指的AnswerButtonA
是方法,而不是按钮。
You need to create an outlet to your button (or use sender
as in Jeremy's answer).
您需要为按钮创建一个出口(或使用sender
Jeremy 的回答)。
From there, here's what you're looking for:
从那里开始,这是您要查找的内容:
answerButtonA.setTitle("Hello World", forState: .Normal)
The method signature will pop up as you're typing:
键入时会弹出方法签名:
回答by Jeremy Pope
The button being pressed is being sent as an argument called sender. AnswerButtonA is the method which you created. Just a style note, usually the first letter is lowercase for methods. So again, AnswerButtonA is the method that is called when you click the button, sender is the actual button clicked.
被按下的按钮作为一个名为 sender 的参数发送。AnswerButtonA 是您创建的方法。只是一个样式说明,通常方法的第一个字母是小写的。同样,AnswerButtonA 是单击按钮时调用的方法,sender 是实际单击的按钮。
IBAction func AnswerButtonA(sender: UIButton)
{
//sender is the button that was pressed
sender.setTitle("HelloWorld", forState: .Normal)
}
回答by rakeshbs
You can try this
你可以试试这个
@IBOutlet answerButtonA : UIButton!
@IBAction func answerButtonAClicked(sender: UIButton)
{
answerButtonA.setTitle("YourTitle", forState: UIControlState.Normal)
}
Also read the swift style guide for learning conventions of variables names and class names. https://github.com/raywenderlich/swift-style-guide
另请阅读 swift 风格指南以了解变量名称和类名称的约定。https://github.com/raywenderlich/swift-style-guide
回答by Indrajeet
Try this code, this definitely helps you
试试这个代码,这肯定对你有帮助
@IBAction func AnswerButtonA (sender: UIButton!)
{
sender.setTitle("Hello World", forState: UIControlState.Normal)
}
回答by Ahmadiah
This one works as well
这个也能用
(sender as AnyObject).setTitle("Un-Down", for: .normal)
回答by Gabriel
sender.title = "my text here"
for Xcode 9.3.1
对于 Xcode 9.3.1