0

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.

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. 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?

Guillaume Algis
  • 10,705
  • 6
  • 44
  • 72
HusseinB
  • 1,321
  • 1
  • 17
  • 41
  • [this looks like a duplicate of this. if not let me know i will delete my reply.][1] [1]: http://stackoverflow.com/questions/18234797/uibutton-setattributedtitle-forstate-not-showing-up – bvsss Jun 25 '14 at 10:36
  • @bvsss thanks for your reply, not it wasn't a duplicate. I will post my solution now. – HusseinB Jun 25 '14 at 10:46

1 Answers1

0

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.

HusseinB
  • 1,321
  • 1
  • 17
  • 41