2013-10-09 12 views
5

私はビューを持っており、プログラムで8つのボタンを作成しました。ボタンのタイトル色は白色です。クリックするとボタンのタイトルの色を緑色に変更したい他のボタンをクリックすると、以前にクリックされたボタンのタイトルカラーが白くなり、現在のボタンのタイトルカラーが緑色になります。クリックするとUIButtonのタイトルカラーを変更する方法は?

どうすればよいですか?

+0

これは実際のものですあなたは重複した質問ではありません。正解ありがとう@Bordz –

答えて

11

は、ボタンの

-(void)onclick:(id)sender{ 
UIButton *button = (UIButton *)sender; 
button.selected = !button.selected; 
} 
0

すべてのボタンに対してIBActionを作成し、プロパティ@property (strong, nonatomic) UIButton *currentButtonを作成します。アクションでは次の操作を行います

-(IBAction)buttonClicked:(id)sender 
{ 
    UIButton *buttonSender = (UIButton *)sender; 

    [self.currentButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    self.currentButton = buttonSender; 
    [self.currentButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 
} 
0

設定制御状態をクリックしたとき、ボタンの選択状態を変更し、この

[mybutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[mybutton setTitleColor:[UIColor greenColor] forState:UIControlStateSelected]; 
[mybutton addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside]; 

のように、すべてのボタンを初期化します。

[btnOk setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected]; 
[btnOk setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted]; 
[btnOk setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal]; 
関連する問題