ios 是否可以以编程方式更新 UIButton 标题/文本?

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

is it possible to update UIButton title/text programmatically?

iosiphoneuibuttonuicontrolstate

提问by George Armhold

I have a UIButton, that when pressed, brings up a new view where the user can change some settings. When the view is dismissed, I'd like to update the title/text of the UIButtonto reflect the new state. I'm calling:

我有一个UIButton,按下后会显示一个新视图,用户可以在其中更改某些设置。当视图被关闭时,我想更新 的标题/文本UIButton以反映新状态。我打电话:

[myButton setTitle: @"myTitle" forState: UIControlStateNormal];
[myButton setTitle: @"myTitle" forState: UIControlStateApplication];
[myButton setTitle: @"myTitle" forState: UIControlStateHighlighted];
[myButton setTitle: @"myTitle" forState: UIControlStateReserved];
[myButton setTitle: @"myTitle" forState: UIControlStateSelected];
[myButton setTitle: @"myTitle" forState: UIControlStateDisabled];

But it never seems to change from the original text/title as specified in IB.

但它似乎永远不会改变 IB 中指定的原始文本/标题。

采纳答案by Ken Pespisa

Do you have the button specified as an IBOutlet in your view controller class, and is it connected properly as an outlet in Interface Builder (ctrl drag from new referencing outlet to file owner and select your UIButton object)? That's usually the problem I have when I see these symptoms.

您是否在视图控制器类中将按钮指定为 IBOutlet,并且它是否作为接口生成器中的插座正确连接(ctrl 从新引用插座拖到文件所有者并选择您的 UIButton 对象)?当我看到这些症状时,这通常是我遇到的问题。



Edit: While it's not the case here, something like this can also happen if you set an attributed titleto the button, then you try to change the title and not the attributed title.

编辑:虽然这里不是这种情况,但如果您为按钮设置了属性标题,然后您尝试更改标题而不是属性标题,也会发生类似的情况。

回答by Yotes

I solved the problem just setting the title parameter for UIControlStateNormal, and it automatically works on the other states. The problem seems to be when you set another UIControlState.

我只为 UIControlStateNormal 设置了标题参数就解决了这个问题,它会自动在其他状态下工作。问题似乎出在您设置另一个 UIControlState 时。

[myButton setTitle: @"myTitle" forState: UIControlStateNormal];

回答by Vlad Spreys

As of Swift 4:

从 Swift 4 开始:

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

回答by Oscar

I discovered another problem. It may be a bug introduced in iOS 5, but I thought I'd point it out for anyone else who encounters it.

我发现了另一个问题。这可能是 iOS 5 中引入的一个错误,但我想我会向遇到它的其他人指出它。

If you don't set any default text for the button in the XIB, no text will ever appear if you set it programmatically. And if you do set text in the XIB, any text you subsequently assign to the button programmatically will be truncated to the size of the default text.

如果您没有为 XIB 中的按钮设置任何默认文本,则如果您以编程方式设置它,则不会出现任何文本。如果您确实在 XIB 中设置了文本,则您随后以编程方式分配给按钮的任何文本都将被截断为默认文本的大小。

And finally, if you're showing the view with your button and then invoke another view (like an ActionSheet) and then dismiss it, the text that you assigned to the button programmatically will be erased and the button caption will return to whatever you set up in the XIB.

最后,如果您使用按钮显示视图,然后调用另一个视图(如 ActionSheet)然后关闭它,您以编程方式分配给按钮的文本将被删除,按钮标题将恢复为您设置的任何内容在XIB。

回答by Jesse Black

Even though Caffeine Coma's issue was resolved, I would like to offer another potential cause for the title not showing up on a UIButton.

尽管 Caffeine Coma 的问题已解决,但我想为 UIButton 上未显示标题提供另一个潜在原因。

If you set an image for the UIButton using

如果您使用 UIButton 设置图像

- (void)setImage:(UIImage *)image forState:(UIControlState)state

It can cover the title. I found this out the hard way and imagine some of you end up reading this page for the same reason.

它可以覆盖标题。我很艰难地发现了这一点,并想象你们中的一些人最终出于同样的原因阅读了此页面。

Use this method instead

改用这个方法

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

for the button image and the title will not be affected.

按钮图像和标题不会受到影响。

I tested this with programmatically created buttons and buttons created in a .xib

我使用以编程方式创建的按钮和在 .xib 中创建的按钮对此进行了测试

回答by Dara Tith

for swift :

快速:

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

回答by CPD

Turns out the docs tell you the answer! The UIButton will ignore the title change if it already has an Attributed String to use (with seems to be the default you get when using Xcode interface builder).

原来文档告诉你答案!如果 UIButton 已经有一个要使用的属性字符串,它将忽略标题更改(这似乎是您在使用 Xcode 界面构建器时获得的默认值)。

I used the following:

我使用了以下内容:

[self.loginButton 
     setAttributedTitle:[[NSAttributedString alloc] initWithString:@"Error !!!" attributes:nil] 
     forState:UIControlStateDisabled];
[self.loginButton setEnabled:NO];

回答by Ramsey

One more possible cause is this:

另一个可能的原因是:

If you attempt to set the button's title in the (id)initWithNibName: ...method, then you're button property will still be nil. It hasn't yet been assigned to the UIButton.

如果您尝试在(id)initWithNibName: ...方法中设置按钮的标题,那么您的按钮属性仍然为零。它尚未分配给 UIButton。

You must be sure that you're setting your buttons in a method like (void)viewWillLoador (void)viewWillAppear, but you probably don't want to set them as late as (void)viewDidAppear.

您必须确保您正在使用(void)viewWillLoad或 之类的方法设置按钮(void)viewWillAppear,但您可能不想在(void)viewDidAppear.

回答by Mike Rapadas

Sometimes it can get really complicated. The easy way is to "refresh" the button view!

有时它会变得非常复杂。简单的方法是“刷新”按钮视图!

//Do stuff to your button here.  For example:

[mybutton setEnabled:YES];

//Refresh it to new state.

[mybutton setNeedsDisplay];

回答by daleijn

@funroll is absolutely right. Here you can see what you will need Make sure function runs on main thread only. If you do not want deal with threads you can do like this for example: create NSUserDefaults and in ViewDidLoad cheking condition was pressed button in another View or not (in another View set in NSUserDefaults needed information) and depending on the conditions set needed title for your UIButton, so [yourButton setTitle: @"Title" forState: UIControlStateNormal];

@funroll 是绝对正确的。在这里你可以看到你需要什么确保函数只在主线程上运行。如果您不想处理线程,您可以这样做,例如:创建 NSUserDefaults 并在 ViewDidLoad 中检查条件是否在另一个视图中按下按钮(在 NSUserDefaults 中设置的另一个视图中需要信息)并根据条件设置所需的标题你的 UIButton,所以 [yourButton setTitle: @"Title" forState: UIControlStateNormal];