ios 即使我在主线程中更新 UIButton 标题文本也不会更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19973515/
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
UIButton title text is not updated even if I update it in Main thread
提问by neutrino
I'm trying to change the title of an UIButton
I've created programmatically, when the user clicks in it. So, this is my code to create the UIButton
:
UIButton
当用户单击它时,我试图更改我以编程方式创建的标题。所以,这是我创建的代码UIButton
:
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
[myButton setBackgroundColor:[UIColor blackColor]];
[myButton setAlpha:0.7];
[myButton setTitle:@"Hello" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(userClicked:) forControlEvents:UIControlEventTouchUpInside];
[parentView addSubview:myButton];
And, in my userClicked:
method, I do:
而且,在我的userClicked:
方法中,我这样做:
-(void) userClicked:(UIButton*)button
{
NSLog(@"USER CLICKED!!!");
if ([NSThread isMainThread])
{
NSLog(@"is main thread");
}
[button setTitle:@"Bye" forState:UIControlStateHighlighted];
[button setTitle:@"Bye" forState:UIControlStateNormal];
[button setTitle:@"Bye" forState:UIControlStateSelected];
[self someLengthyComputation];
}
The weird thing is that I can see the log messages printed:
奇怪的是我可以看到打印的日志消息:
USER CLICKED!!!
isMainThread
But, the title of the button does not change! What am I doing wrong?
但是,按钮的标题不会改变!我究竟做错了什么?
EDIT: Setting the title for several states doesn't work either.
编辑:为几个州设置标题也不起作用。
EDIT2: If I print the description of button in the debugger window of Xcode, it shows the right title!
EDIT2:如果我在 Xcode 的调试器窗口中打印按钮的描述,它会显示正确的标题!
Printing description of button->_titleView:
<UIButtonLabel: 0xa4c9310; frame = (95 216; 130 22); text = 'Bye'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xa44f080>>
回答by Walter Schurter
This worked for me to update the title text (iOS 7.1, Xcode 5.1):
这对我更新标题文本有用(iOS 7.1,Xcode 5.1):
button.enabled = FALSE;
[button setTitle:@"Test" forState:UIControlStateNormal];
button.enabled = TRUE;
回答by Jeffrey Sun
I was having the same problem, and I noticed that everywhere else I was setting the attributedTitle. So any updates to the title were not affecting the attributed title.
我遇到了同样的问题,我注意到我在其他地方设置了属性标题。因此,对标题的任何更新都不会影响属性标题。
My solution:
我的解决方案:
[button setAttributedTitle:@"" forState:UIControlStateNormal];
instead of
代替
[button setTitle:@"" forState:UIControlStateNormal];
回答by Lukasz
At the moment, the shortest work around I came with is calling:
目前,我带来的最短工作是调用:
[button setNeedsLayout];
After updating the title.
更新标题后。
This seems to happen in iOS 7.1. All my buttons, which were behaving correctly in previous iOS versions (or maybe just compiled with previous SDKs) suddenly stopped doing that when compiled in Xcode 5.1.1 SDK 7.1.
这似乎发生在 iOS 7.1 中。在 Xcode 5.1.1 SDK 7.1 中编译时,我所有在以前的 iOS 版本(或者可能只是用以前的 SDK 编译)中正常运行的按钮突然停止这样做。
Looks like Apple's bug.
看起来像苹果的错误。
回答by user3000868
I had a similar problem using storyboards. Using the answers above I had to use
我在使用故事板时遇到了类似的问题。使用上面的答案我不得不使用
[mybutton setTitle:@"SomeText" forState:UIControlStateNormal];
[button setNeedsLayout];
[button layoutIfNeeded];
AND I had to make sure that the button type was 'Custom' not 'System' in the attributes inspector.
而且我必须确保属性检查器中的按钮类型是“自定义”而不是“系统”。
回答by bachman
Please see if this might help you...when the button is clicked check for condition if buttonToggled...like below when you have a function like changeButtonText
请看看这是否对你有帮助...当按钮被点击时检查条件 if buttonToggled...像下面这样,当你有一个像 changeButtonText 这样的函数时
-(IBAction)changeButtonText:(id)sender {
if (!buttonToggled) {
[sender setTitle:@"Initial Text" forState:UIControlStateNormal];
buttonToggled = YES;
} else {
[sender setTitle:@"Alternative Text" forState:UIControlStateNormal];
buttonToggled = NO;
}
}
回答by Eugene Tartakovsky
There are several issues in your code:
您的代码中有几个问题:
You are assigning callback to the button:
您正在为按钮分配回调:
@selector(userClicked:)
but your code is in another method:
但您的代码采用另一种方法:
-(void)userTapOnTapToRefreshView:(UIButton*)button
To fix that you need to implement something like this:
要解决此问题,您需要实现以下内容:
-(void)userClicked:(id)sender
{
[(UIButton*)sender setTitle:@"Bye" forState:UIControlStateNormal];
}
Also this part of code does not make sense for me:
这部分代码对我来说也没有意义:
[parentView myButton];
Try to change it to:
尝试将其更改为:
[parentView addSubview:myButton];
Hope it will help :)
希望它会有所帮助:)
回答by dragonflyesque
This is kinda late and somewhat relates to Walter Schurter's response:
这有点晚了,有点与 Walter Schurter 的回应有关:
I had a similar issue where my button text was being set correctly until I updated to 7.1. Then I found that since my button was disabled, I had to set the title color and title text for the disabled state to get the text to show up. Then everything worked like a charm!
我有一个类似的问题,在我更新到 7.1 之前,我的按钮文本设置正确。然后我发现由于我的按钮被禁用,我必须为禁用状态设置标题颜色和标题文本才能显示文本。然后一切都像魅力一样!
回答by ccoroom
In iOS 7, UIButton's title is not updated when it is disabled. It seems like a bug on iOS 7.
在 iOS 7 中,UIButton 的标题在禁用时不会更新。这似乎是 iOS 7 上的一个错误。
As a workaround, update both normal and disabled title. It works!
作为解决方法,更新正常和禁用的标题。有用!
[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateDisabled];
回答by zero3nna
For Swift 3to 5just use the following:
对于Swift 3到5,只需使用以下内容:
button.setTitle("My title", for: .normal)
or for a attributed text use this:
或者对于属性文本使用这个:
button.setAttributedTitle(<AttributedString>, for: .normal)
回答by Nikos M.
Try this:
尝试这个:
[button setTitle:@"Bye" forState:UIControlStateHighlighted];
or:
或者:
[button setTitle:@"Bye" forState:UIControlStateSelected];
You could also modify your void function:
您还可以修改您的 void 函数:
-(void) userClicked
{
NSLog(@"USER CLICKED!!!");
if ([NSThread isMainThread])
{
NSLog(@"is main thread");
}
[myButton setTitle:@"Bye" forState:UIControlStateNormal];
}