单击第一个按钮时,它变为高亮,单击第二个时,第二个突出显示,第一个变为正常
When clicking first button,it becomes highlighted and while clicking second,second highlighted and first become normal
我正在创建一个包含五个按钮的应用程序,已创建programmatically.The以下是要求...
点击第一个按钮时它保持不变 highlighted.while点击第二个按钮,第一个变为正常,第二个保持突出显示...即单击的特定按钮变为突出显示,所有其他按钮保持正常......请帮助..
我的解决方案是:
获取一个 属性 以跟踪所选按钮
@property (nonatomic, weak) UIBUtton *lastSelectedButton;
在您的按钮回调方法中(您在添加按钮时配置的选择器):
- (void)didClickButton:(UIButton *)button {
if (self.lastSelectedButton isEqual:button) {
// Don't need to do anything in this case because the button is already selected
return;
}
[self.lastSelectedButton setSelected:NO];
[button setSelected:YES];
[self setLastSelectedButton:button];
}
如果您有任何问题或需要更多帮助,请告诉我!
您可以创建一个实例变量 int
来保存标签值。现在无论你点击哪个按钮,你都需要清除前一个按钮的标签值并将其分配给当前按钮。
例如:
int const TAG_HIGHLIGHTED_BUTTON = 100;
- (void)buttonAction:(UIButton *)button {
UIButton *prevButton = [self.view viewWithTag:TAG_HIGHLIGHTED_BUTTON];
[prevButton setTag:0];
[button setTag:TAG_HIGHLIGHTED_BUTTON];
}
可以设置
[button setBackgroundImage:[UIImage imageNamed:@"normalbackgroundimage"] forState:UIControlStateNormal]; //not highlighted
[button setBackgroundImage:[UIImage imageNamed:@"highlightedbackgroundimage"] forState:UIControlStateHighlighted | UIControlStateSelected]; //highlighted
现在,当您设置 button.selected = YES
时,它会突出显示
所以你可以在点击按钮时做这样的事情
button1.selected = YES;
otherButton.selected = NO;
编辑
解决您的问题的简单方法是创建一个 ibaction 将其连接到您想要的所有按钮 selected/deselect 然后执行此操作
-(IBAction)someButtonPressed:(UIButton*)sender{
button1.selected = NO;
button2.selected = NO;
button3.selected = NO; //and so on...just set all your buttons to selected = NO
//at the end you just select button you clicked
sender.selected = YES;
}
我正在创建一个包含五个按钮的应用程序,已创建programmatically.The以下是要求...
点击第一个按钮时它保持不变 highlighted.while点击第二个按钮,第一个变为正常,第二个保持突出显示...即单击的特定按钮变为突出显示,所有其他按钮保持正常......请帮助..
我的解决方案是:
获取一个 属性 以跟踪所选按钮
@property (nonatomic, weak) UIBUtton *lastSelectedButton;
在您的按钮回调方法中(您在添加按钮时配置的选择器):
- (void)didClickButton:(UIButton *)button {
if (self.lastSelectedButton isEqual:button) {
// Don't need to do anything in this case because the button is already selected
return;
}
[self.lastSelectedButton setSelected:NO];
[button setSelected:YES];
[self setLastSelectedButton:button];
}
如果您有任何问题或需要更多帮助,请告诉我!
您可以创建一个实例变量 int
来保存标签值。现在无论你点击哪个按钮,你都需要清除前一个按钮的标签值并将其分配给当前按钮。
例如:
int const TAG_HIGHLIGHTED_BUTTON = 100;
- (void)buttonAction:(UIButton *)button {
UIButton *prevButton = [self.view viewWithTag:TAG_HIGHLIGHTED_BUTTON];
[prevButton setTag:0];
[button setTag:TAG_HIGHLIGHTED_BUTTON];
}
可以设置
[button setBackgroundImage:[UIImage imageNamed:@"normalbackgroundimage"] forState:UIControlStateNormal]; //not highlighted
[button setBackgroundImage:[UIImage imageNamed:@"highlightedbackgroundimage"] forState:UIControlStateHighlighted | UIControlStateSelected]; //highlighted
现在,当您设置 button.selected = YES
时,它会突出显示
所以你可以在点击按钮时做这样的事情
button1.selected = YES;
otherButton.selected = NO;
编辑
解决您的问题的简单方法是创建一个 ibaction 将其连接到您想要的所有按钮 selected/deselect 然后执行此操作
-(IBAction)someButtonPressed:(UIButton*)sender{
button1.selected = NO;
button2.selected = NO;
button3.selected = NO; //and so on...just set all your buttons to selected = NO
//at the end you just select button you clicked
sender.selected = YES;
}