0
我試圖改變一個按鈕的屬性稱號。更改應該在NSURLConnection
請求之後發生,因此它被放置在完成處理程序中。不能更改的UIButton歸因標題
當應用程序打開時,代碼運行良好,但是在代碼中的某處我想再次更改標題,但它不會更改。下面是我在做什麼:
[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的變化,我不希望它被推遲。現在當我稍後運行代碼時,標題不會改變,但如果我點擊按鈕,它會改變。它出什麼問題了?
[這看起來像一個重複的。如果不是讓我知道我會刪除我的回覆] [1] [1]:http://stackoverflow.com/questions/18234797/uibutton-setattributedtitle-forstate-not-showing-up – bvsss
@ bvsss感謝您的回覆,而不是重複。我現在會發布我的解決方案。 – HusseinB