簡體   English   中英

UIButton所選文本未顯示為所選狀態

[英]UIButton selected text not showing for selected state

所有,

我有一個包含按鈕的UI元素。 我在按鈕上設置文本,並同時設置所選狀態。 見下文:

- (void)setText:(NSString *)text {
    self.inputButton.selected = text != nil; // select the buttons if the text is not nil
    self.cancelButton.selected = text != nil;

    [self.inputButton setTitle:text forState:UIControlStateSelected];
    [self.inputButton setTitle:text forState:UIControlStateHighlighted];
}

在視圖控制器中填寫后,此控件將正常工作。

但是,當我從上一個視圖控制器傳遞文本時,所選狀態文本不會顯示。 cancelButton selected狀態可以正常工作,並且應該選擇了inputButton狀態,但是,所選狀態文本不會顯示。

我不知道這是因為按鈕沒有重畫還是什么? 我已經嘗試了所有的[self layoutIfNeed]和[self setNeedsDisplay]都沒有運氣。 有人知道發生了什么嗎?

我發現了問題。 我同時使用了選中狀態和突出顯示狀態。 由於某些時髦的原因,這造成了按鈕狀態可能為5的情況。我通過不以任何方式解決UIControlStateHighlighted來解決了該問題。

您可以檢查是否添加以下代碼行。

- (void)setText:(NSString *)text {
    self.inputButton.selected = text != nil; // select the buttons if the text is not nil
    self.cancelButton.selected = text != nil;

    [self.inputButton setTitle:text forState:UIControlStateSelected];
//Start:Added line of Code
    [self.inputButton setTitle:text forState:UIControlStateNormal];
//End:
    [self.inputButton setTitle:text forState:UIControlStateHighlighted];
}

感謝和問候,

阿米特

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM