簡體   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