我有一个典型的要求,我需要按下按钮后突出显示状态.我需要执行一个任务,只有当一个按钮处于突出显示状态时才应该工作.其实我正在设置一个按钮状态,以编程方式突出显示.
[发件人设置高亮:YES];
一旦按钮处于突出显示状态,我需要执行另一个操作.
- - (IBAction)changeState: (UIButton*)sender
- {
- if (sender.highlighted == YES)
- {
- [self performSomeAtion:sender];
- }
- }
但是,对于我的恐惧,每当我按任何按钮,上述情况变得真实,并且重复执行动作.有没有什么办法可以保持UIButton的状态被压制后突出显示?
编辑 – 实际上,我需要对3个不同的按钮状态执行3种不同的动作.我已经使用了选择的状态和正常状态.现在,我需要利用突出显示的状态.
解决方法
- [sender setSelected:YES];
或者您可以使用UIButton(notselectedimage.png和selectedimage.png)的两个图像来模拟此效果,然后使用BOOL buttonCurrentStatus等BOOL变量跟踪按钮状态.然后在.h文件中
- BOOL buttonCurrentStatus;
和.m文件
- // connect this method with Touchupinside function
- - (IBAction)changeState:(UIButton*)sender
- {
- /* if we have multiple buttons,then we can
- differentiate them by tag value of button.*/
- // But note that you have to set the tag value before use this method.
- if([sender tag] == yourButtontag){
- if (buttonCurrentStatus == NO)
- {
- buttonCurrentStatus = YES;
- [butt setImage: [UIImage imageNamed:@"selectedImage.png"] forState:UIControlStateNormal];
- //[self performSomeAction:sender];
- }
- else
- {
- buttonCurrentStatus = NO;
- [butt setImage:[UIImage imageNamed:@"notSelectedImage.png"] forState:UIControlStateNormal];
- //[self performSomeAction:sender];
- }
- }
- }