2013-02-15 21 views
0

目標是在UIButton單擊時更改我的UITextView的字體顏色和背景顏色,然後在再次單擊UIButton時將它們還原爲原始顏色;然後重複,等等。更改UIButton按鈕上的現有設置UIColors按

Screenshot

默認顏色設置

  • 文字:blackColor
  • 背景:lightGrayColor

在按鈕點擊(改變..)

  • 文字:greenColor
  • 背景:blackColor

(這時如果再次點擊,變回默認的顏色設置)

到目前爲止,我有這樣的:

- (IBAction) enableSleepMode:(id)sender { 

    [txtNotes setTextColor:[UIColor greenColor]]; 
    [txtNotes setBackgroundColor:[UIColor blackColor]]; 

} 

我道歉,我不確定該從哪裏出發。提前致謝。

+1

恰好是不工作怎麼辦?該代碼似乎將它的工作設置爲綠色/黑色?你可以做一個'if(txtNotes.textColor == [UIColor greenColor])'來確定它的進入和交換的狀態... – Shizam 2013-02-15 20:26:58

回答

1

你說改變「UITextView」而不是「UIButton」權的字體顏色和背景顏色? 。那你添加了UItextViewDelegate嗎?

我已經這樣做了類似

編輯:

- (void)viewDidLoad 
    { 
    [super viewDidLoad]; 
    [txtView setBackgroundColor:[UIColor lightGrayColor]]; 
    [txtView setTextColor:[UIColor blackColor]]; 
    str = @"first"; 
    } 

    -(IBAction)bt:(id)sender 
{ 
    if([str isEqualToString:@"first"]) 
    { 
     [txtView setBackgroundColor:[UIColor blackColor]]; 
     [txtView setTextColor:[UIColor greenColor]]; 
     str = @"second"; 
    } 
    else 
    { 
     [txtView setBackgroundColor:[UIColor lightGrayColor]]; 
     [txtView setTextColor:[UIColor blackColor]]; 
     str = @"first"; 
    } 
} 
+0

啊!這很美麗,謝謝!如果我在原始文章中沒有說清楚,我表示歉意。今晚我會在我進入我的工作站時嘗試一下。 – 2013-02-17 16:24:12

+0

它現在說'物品'被選中'在物品UIBarButtonItem上找不到'http://content.screencast.com/users/revblaze/folders/Jing/media/a689747a-353b-4eb5-8c22-aab0e1fe842c/00000089.png – 2013-02-17 20:52:57

+0

注意:我確實已經連接了所有發送的動作和引用插座。 – 2013-02-17 21:00:03

1

試試這個

(void)setTextColor:(UIColor *)color forState:(UIControlState)state 

[button setTextColor:[UIColor greenColor] forState:UIControlStateNormal]; 
[button setTextColor:[UIColor blackColor] forState:UIControlStateHighlighted]; 

(void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state 

[button setBackgroundColor:[UIColor blackColor] forState:UIControlStateNormal]; 
[button setBackgroundColor:[UIColor lightgrayColor] forState:UIControlStateHighlighted]; 

類似的東西是我想你在找什麼

+0

是的,那就是我一直在尋找的!現在我該如何實現這一點,因爲無論我在哪裏添加它,我都會返回2個錯誤:'使用未聲明的標識符setTextColor'和'使用未聲明標識的setBackgroundColor' 不是它目前在我的_enableSleepMode_ IBAction下。 – 2013-02-15 22:38:04

+0

好笑。你是否適當地重命名按鈕?我知道它工作杉木標題/酒吧。有一個更復雜的方法來做到這一點。請參閱http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uitextfield-uitextfielddelegate/。在他們的示例中,當文本字段處於活動狀態時,背景變爲綠色。它涉及到添加一個布爾值。 – 2013-02-16 04:11:16

+0

另一方面請參閱http://stackoverflow.com/questions/2270526/uisegmentedcontrol-selected-segment-color/5932524#5932524 – 2013-02-16 04:18:00