ios – 如何在按下UIButton状态后突出显示

我有一个典型的要求,我需要按下按钮后突出显示状态.我需要执行一个任务,只有当一个按钮处于突出显示状态时才应该工作.其实我正在设置一个按钮状态,以编程方式突出显示.

[发件人设置高亮: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];
    }   
  }
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...