我知道这个问题经常被问到,也有很多正确的答案,但我在我的代码中遇到了一个问题。我有一些单选按钮,当用户点击按钮时,背景颜色和文本颜色会改变。背景颜色改变得如我所预期,但是文本颜色在按钮被点击时变成了蓝色区域。我已经编写了代码,在点击时将其更改为白色,但是当我点击按钮时,它会将文本区域改为蓝色,我不知道我犯了什么错误。我的代码如下:
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];
}
}