2010-03-19 105 views
3

我想通過代碼設置UIButton的標題。我發現自己必須爲UIControlStateNormal,UIControlStateHighlighted,UIControlStateDisabled和UIControlStateSelected調用 - [UIButton setTitle:forState:]。而這甚至不考慮這些國家的所有組合。使用單個方法調用設置UIButton的標題?

不用說,這是令人厭煩的。是否有一個我可以做的調用,將設置一個字符串作爲所有狀態的標題? (因爲,我假設在95%的情況下,這是所需的行爲?)

回答

9

是的,你當然可以。 From the docs

在一般情況下,如果一個屬性是不是一個國家規定 ,默認是 使用UIControlStateNormal值。 如果未設置UIControlStateNormal 的值,則屬性將默認爲 爲系統值。因此,至少在 處,應該將 的值設置爲正常狀態。

所以,只需設置標題UIControlStateNormal,你就是金。

+5

在實踐中,我發現情況似乎並非如此:文本神祕地消失,當一個按鈕被點擊等。這就是說,我不知道我可以重新創建這些情況。如果我發現問題,我會在這裏發佈。謝謝! – 2010-03-19 18:09:09

7

像Mr./Ms。青蛙說,設置標題爲UIControlStateNormal通常會做伎倆。唯一的例外是如果標題已經設置爲其他狀態。 UIControlState是一個面具,這樣你就可以掩蓋你的屁股像這樣:

[button setTitle:@"Title" forState:UIControlStateNormal|UIControlStateHighlighted| UIControlStateDisabled|UIControlStateSelected] 

如果你試圖要簡潔:

#define kAllControlStates (UIControlStateNormal|UIControlStateHighlighted| UIControlStateDisabled|UIControlStateSelected) 
[button setTitle:@"Title" forState:kAllControlStates]; 

還是簡潔和不透明:

[button setTitle:@"Title" forState:0xffff]; 

更新:我應該在回答之前測試過。事實證明,像UIControlStateHighlighted|UIControlStateDisabled這樣的掩碼錶示控件同時高亮顯示禁用的狀態。我錯誤地認爲該掩碼錶示「高亮已禁用」。總而言之,你最好用青蛙先生的回答。

+4

我發現這不適合我。文字沒有顯示在按鈕上。我想知道是否因爲UIControlStateNormal等於0,使它在掩碼中相當無用? – 2010-04-14 21:22:13

+0

Mr. Frog :) 15chars – 2010-04-14 23:18:45

+0

@George:你說得對。看起來我的回答不正確。我會添加一個註釋。 – Tom 2010-04-14 23:59:58

0

爲UIButton創建一個類別方法,一次爲所有狀態設置它。

0

雖然回落到UIControlStateNormal狀態的值可能已經足夠99%的時間了,但我已經爲1%用例提出瞭解決這個問題的方案。 (我有一個子類中的應用程序定義狀態,這意味着回到正常狀態將是不正確的。)

即時回覆已被接受,我在此發佈它的完整性。基本上,它爲狀態設置屬性,以及您指定的各個附加狀態的每個組合。

@interface UIButton (AdditionalStates) 
- (void)setTitle:(NSString *)title forState:(UIControlState)state additionalStates:(UIControlState)additionalStates; 
@end 

@implementation UIButton (AdditionalStates) 

- (void)setTitle:(NSString *)title forState:(UIControlState)state additionalStates:(UIControlState)additionalStates 
{ 
    [self setValue:title forKey:@"title" state:state additionalStates:additionalStates mask:(1 << 0)]; 
} 

- (void)setValue:(id)value forKey:(NSString *)key state:(UIControlState)state additionalStates:(UIControlState)additionalStates mask:(NSUInteger)mask(UIControlState)additionalStates 
{ 
    if (additionalStates == 0) { 
     [self setValue:value forKey:key state:state]; 
     return; 
    } 

    // Iterate over each 'on' bit in additionalStates, starting from the mask bit 
    while (mask > 0) { 
     if (additionalStates & mask) { 
      // Delete the current bit from additionalStates 
      NSUInteger reducedAdditionalStates = (additionalStates^mask); 

      // Set the title for combinations of the remaining additional states with and without the mask bit 
      [self setValue:value forKey:key state:(state | (additionalStates & mask)) additionalStates:reducedAdditionalStates mask:(mask << 1)]; 
      [self setValue:value forKey:key state:state additionalStates:reducedAdditionalStates mask:(mask << 1)]; 
     } 
     mask = (mask << 1); 
    } 
} 

- (void)setValue:(id)value forKey:(NSString *)key state:(UIControlState)state 
{ 
    if ([key isEqualToString:@"title"]) { 
     [self setTitle:value forState:state]; 
     return; 
    } 
} 

@end