简体   繁体   English

无法更改UIButton属性标题

[英]Can't Change UIButton attributed title

I'm trying to change the attribute title of a button. 我正在尝试更改按钮的属性标题。 The change should take place after an NSURLConnection request, hence it's placed in the completion handler. 更改应在NSURLConnection请求之后进行,因此应将其放置在完成处理程序中。

When the app opens, the code works well, however at some point in code I want to change the title again but it's not changing. 当应用程序打开时,代码运行良好,但是在代码中的某些时候,我想再次更改标题,但它没有改变。 Here is what I am doing: 这是我在做什么:

[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];
    }];
}];

I'm exiting the block on the main queue since it's a UI change and I don't want it to be delayed. 我正在退出主队列中的块,因为这是UI更改,我不希望它被延迟。 Now when I run the code later on, the title doesn't change but if I tap the button it changes. 现在,当我稍后运行代码时,标题不会改变,但是如果我点击按钮,它将改变。 What's wrong with it? 它出什么问题了?

I found out the problem. 我发现了问题。 the button is in the navigation bar and when tapped it opens the side bar. 该按钮位于导航栏中,点击该按钮将打开侧边栏。 When the user swipes it also opens the sidebar however the button is being stuck on Selected. 当用户滑动时,它也会打开侧栏,但是按钮被卡在“选定”上。 Hence since the Attribute title is set on Normal it won't change. 因此,由于“属性”标题设置为“普通”,因此不会更改。

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

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