更改Xcode中的按钮文字? [英] Change button text from Xcode?
问题描述
我有一个IBAction连接到我的界面生成器中的按钮.
I have a IBAction connected to a button in my Interface Builder.
是否可以在运行时从我的代码中更改按钮(在IB中)上的文本?
Is it possible to change the text on the button (in IB) from within my code during runtime?
推荐答案
如果您的代码中有一个与操作相关的按钮,则可以在不使用实例变量的情况下更改标题.
If you've got a button that's hooked up to an action in your code, you can change the title without an instance variable.
例如,如果按钮设置为此操作:
For example, if the button is set to this action:
-(IBAction)startSomething:(id)sender;
您可以在方法中简单地做到这一点:
You can simply do this in the method:
-(IBAction)startSomething:(id)sender {
[sender setTitle:@"Hello" forState:UIControlStateNormal];
}
或者,如果您想切换按钮的名称,则可以创建一个名为"buttonToggled"的BOOL
(例如),并通过以下方式切换名称:
Or if you're wanting to toggle the name of the button, you can create a BOOL
named "buttonToggled" (for example), and toggle the name this way:
-(IBAction)toggleButton:(id)sender {
if (!buttonToggled) {
[sender setTitle:@"Something" forState:UIControlStateNormal];
buttonToggled = YES;
}
else {
[sender setTitle:@"Different" forState:UIControlStateNormal];
buttonToggled = NO;
}
}
这篇关于更改Xcode中的按钮文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!