Ios UIButton标题文本不会更新,即使我在主线程中更新了它

Ios UIButton标题文本不会更新,即使我在主线程中更新了它,ios,objective-c,uibutton,ios7,xcode5,Ios,Objective C,Uibutton,Ios7,Xcode5,我试图在用户单击时更改我通过编程创建的ui按钮的标题。这是我创建ui按钮的代码: myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)]; [myButton setBackgroundColor:[UIColor blackColor]]; [myButton setAlpha:0.7]; [myButton s

我试图在用户单击时更改我通过编程创建的
ui按钮的标题。这是我创建
ui按钮的代码:

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];
在我的
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];
}
奇怪的是,我可以看到日志信息打印出来:

USER CLICKED!!! 
isMainThread
但是,按钮的标题不会更改!我做错了什么

编辑:为几个州设置标题也不起作用

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>>
按钮打印说明->\u标题查看:
试试这个:

[button setTitle:@"Bye" forState:UIControlStateHighlighted];
或:

您还可以修改void函数:

-(void) userClicked
{
    NSLog(@"USER CLICKED!!!");
    if ([NSThread isMainThread])
    {
        NSLog(@"is main thread");
    }

    [myButton setTitle:@"Bye" forState:UIControlStateNormal];
}

请查看这是否有助于您…当单击按钮时,请检查按钮是否已切换的条件…如下图所示,当您具有changeButtonText之类的功能时

-(IBAction)changeButtonText:(id)sender {
    if (!buttonToggled) {
        [sender setTitle:@"Initial Text" forState:UIControlStateNormal];
        buttonToggled = YES;
    } else {
        [sender setTitle:@"Alternative Text" forState:UIControlStateNormal];
        buttonToggled = NO;
    }
}

我怀疑作为参数传入的'button'是否是myButton。无论如何,如果myButton是成员变量,您可以只[myButton setTitle:@“Bye”forState:UIControlStateNormal]

您的代码中有几个问题:

您正在为按钮分配回调:

@selector(userClicked:)
但您的代码使用另一种方法:

-(void)userTapOnTapToRefreshView:(UIButton*)button
要解决此问题,您需要实现如下内容:

-(void)userClicked:(id)sender
{
    [(UIButton*)sender setTitle:@"Bye" forState:UIControlStateNormal];
}
    [myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [myButton setTitle:@"SomeText" forState:UIControlStateNormal];.
此外,这部分代码对我来说没有意义:

[parentView myButton];
尝试将其更改为:

[parentView addSubview:myButton];

希望能有所帮助:)

最后,我终于明白了。有两个问题:

1)
按钮
未处于状态
uicontrol状态正常

2) 我正在调用一个方法,该方法在设置标题后执行长时间计算,
[self someLengthyComputation]

我通过以下方式解决了这个问题:

1) 设置按钮所有状态的标题

2) 在另一个线程(而不是主线程)中执行大型计算

我现在的工作代码是:

-(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];
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        [self someLengthyComputation];
    });
}

非常感谢所有回答/评论的人

这对我更新标题文本起到了作用(iOS 7.1,Xcode 5.1):


这有点晚了,与沃尔特·舒特的回答有些关联:


我有一个类似的问题,我的按钮文本被正确设置,直到我更新到7.1。然后我发现,由于我的按钮被禁用,我必须为禁用状态设置标题颜色和标题文本,以使文本显示出来。然后一切都很顺利

好吧,这都是关于UIButton的启用状态,苹果在7.1中做了一些更改,如果UIButton处于禁用状态,则不允许更改标题,仅此而已

感谢苹果,我已经浪费了10分钟。调试一个运行良好的应用程序


今天早上刚发现,昨天将XCode更新为5.1.1,将iOS更新为7.1。目前,我最短的工作就是打电话:

[button setNeedsLayout];
更新标题后

这似乎发生在iOS 7.1中。 我的所有按钮在以前的iOS版本(或者可能只是用以前的SDK编译)中正常运行,但在Xcode 5.1.1 SDK 7.1中编译时突然停止运行


看起来像是苹果的bug。

根据《苹果开发者指南》,您不应该将按钮标题标签文本或颜色直接设置为属性(例如,do不要这样做:myButton.titleLabel.text=@“SomeText”或myButton.titleLabel.textColor=[UIColor blackColor])

而是使用UIButton setter功能设置它们,如下所示:

-(void)userClicked:(id)sender
{
    [(UIButton*)sender setTitle:@"Bye" forState:UIControlStateNormal];
}
    [myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [myButton setTitle:@"SomeText" forState:UIControlStateNormal];.
…或者像这样:

-(void)userClicked:(id)sender
{
    [(UIButton*)sender setTitle:@"Bye" forState:UIControlStateNormal];
}
    [myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [myButton setTitle:@"SomeText" forState:UIControlStateNormal];.

请参阅。

我也遇到了同样的问题,我注意到我在其他地方都设置了attributedTitle。因此,标题的任何更新都不会影响属性标题

我的解决方案:

[button setAttributedTitle:@"" forState:UIControlStateNormal];
而不是

[button setTitle:@"" forState:UIControlStateNormal];

在设置UIButton上的文本之前,请始终执行addSubview。

这可能是按钮布局刷新问题。。。。。 尝试使用

[button setNeedsLayout];
[button layoutIfNeeded];

它将强制按钮更新布局。

我在使用故事板时遇到类似问题。使用上面的答案,我必须使用

[mybutton setTitle:@"SomeText" forState:UIControlStateNormal];
[button setNeedsLayout];
[button layoutIfNeeded];

我必须确保属性检查器中的按钮类型是“自定义”而不是“系统”

在iOS 7中,UIButton的标题在禁用时不会更新。这似乎是iOS 7上的一个bug

作为一种解决方法,更新普通标题和禁用标题。它起作用了

[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateDisabled];

这可能很简单,但是如果您设置一些填充整个按钮框架的按钮图像(而不是背景图像),这将向右移动标题,从而使其不可见。
如果是这样,请尝试将按钮图像更改为背景图像。

在我的情况下,我尝试了所有方法,但没有任何效果。
然后,我只是将按钮类型从系统更改为从情节提要自定义。繁荣一切开始正常运行。

无需使用:

[button setNeedsLayout];
[button layoutIfNeeded];
相反,首先将类型设置为默认值自定义

如果应用了
setAttributeTitle
,则使用:

[button setAttributedTitle:[NSAttributedString new] forState:UIControlStateNormal];
否则就没有必要做任何改变

如果文本的颜色没有更改,则应用相同的内容并使用以下设置标题颜色:

[button setTitleColor:[any_color] forState:UIControlStateNormal];

在尝试了许多解决方案后得出的结论是使用setAttributedTitle而不是setTitle

要为AttributeString创建标题字符串,请执行以下操作:

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@" new Test "];

[button setAttributedTitle:attString forState:UIControlStateNormal];
顺便说一句,这个问题在正常情况下不会偶尔发生,但它突然发生主要是因为以下原因:

  • 如果更改
    ui按钮的启用状态,并尝试更改标题

  • 如果使用属性值,然后希望使用setTitle对其进行更改,则在这种情况下,属性值优于标题

  • 如果导航到其他视图控制器,然后返回以更新按钮标题


  • 尝试在界面或视图/视图控制器的实现部分将按钮声明为属性

    @property(nonatomic,strong) UIButton* myButton;
    
    然后创建它

    self.myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
    [_myButton setBackgroundColor:[UIColor blackColor]];
    ...
    
    它应该会起作用。别问我为什么,我只是觉得这都是关于ARC/现代内存管理的

    编辑:

    如果您在bu上保留一个参考,它也应该起作用