繁体   English   中英

无法更改UIButton属性标题

[英]Can't Change UIButton attributed title

我正在尝试更改按钮的属性标题。 更改应在NSURLConnection请求之后进行,因此应将其放置在完成处理程序中。

当应用程序打开时,代码运行良好,但是在代码中的某些时候,我想再次更改标题,但它没有改变。 这是我在做什么:

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        NSString *city = [cities firstObject];
        NSMutableAttributedString *subTitleOne = [[NSMutableAttributedString alloc] initWithString:@"something "];
        NSMutableAttributedString *subTitleTwo = [[NSMutableAttributedString alloc] initWithString:city.uppercaseString];
        [subTitleTwo addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.1 green:0.8 blue:0.44 alpha:1]  range:NSMakeRange(0, [NSString stringWithFormat:@"%@", city.uppercaseString].length)];
        [subTitleOne appendAttributedString:subTitleTwo];

        [_sidebarButton setAttributedTitle:subTitleOne forState:UIControlStateNormal];
        [_sidebarButton.titleLabel setAdjustsFontSizeToFitWidth:YES];
    }];
}];

我正在退出主队列中的块,因为这是UI更改,我不希望它被延迟。 现在,当我稍后运行代码时,标题不会改变,但是如果我点击按钮,它将改变。 它出什么问题了?

我发现了问题。 该按钮位于导航栏中,点击该按钮将打开侧边栏。 当用户滑动时,它也会打开侧栏,但是按钮被卡在“选定”上。 因此,由于“属性”标题设置为“普通”,因此不会更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM