Reputation: 189
I know this question is asked frequently and many of answers are correct, I have my code in which I'm facing an issue. I have radio buttons on which when user click the button background color and text color changes. The background color is changed as I expected but the text color turns blue area when the button is clicked. I have coded it to change white color when click but when I click the button it changes the text area to blue, I'm confused that where I'm doing mistakes. My code is,
self.selectedTitleColor = [UIColor whiteColor];
self.selectedBackgroundColor = [UIColor redColor];
self.unselectedTitleColor = [UIColor blackColor];
self.unselectedBackgroundColor = [UIColor whiteColor];
- (IBAction)House:(id)sender {
[sender setSelected: ![sender isSelected]];
[self.flat setSelected:NO];
[self.lowerP setSelected:NO];
[self.upperP setSelected:NO];
[self.farmH setSelected:NO];
[self.pentH setSelected:NO];
[self.roomB setSelected:NO];
[self updateButtonColors];
}
-(void)updateButtonColors {
if (self.house.selected) {
[self.house setTitleColor:self.selectedTitleColor forState:UIControlStateNormal];
[self.house setBackgroundColor:self.selectedBackgroundColor];
House=_house.titleLabel.text;
ImageViewD = [NSString stringWithFormat:@"%@",House];
NSLog(@"HHHH %@",ImageViewD);
} else {
[self.house setTitleColor:self.unselectedTitleColor forState:UIControlStateNormal];
[self.house setBackgroundColor:self.unselectedBackgroundColor];
}
}
Upvotes: 3
Views: 1619
Reputation: 5186
Best to give all property into Storyboard itself. You can take two images for your color and set into UIButton's backgroundImage state.
Check out below image for same.
[]
I have a binding button with below method and its code:
- (IBAction)clicked:(id)sender {
UIButton *btn = (UIButton *)sender;
btn.selected = !btn.selected;
}
Below are images which I took it for reference.
Hope it will help you.
Upvotes: 0