按钮的选中与非选中的状态的切换

#import "ViewController.h"

#import "UIView+HGYFrame.h"

#import "UIButton+HGYButton.h"

@interface ViewController ()

@property(nonatomic,strong)UIButton *selectedButton;

@property( nonatomic,assign)NSInteger  selectBtnTag;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.


    NSArray *titles = @[@"离我最近",@"好评优选",@"人气最高"];

    for(NSIntegeri =0; i<3; i++) {


        UIButton*button = [[UIButtonalloc]init];

        button.tag= i;

        button.height=16;

        button.width=self.view.frame.size.width/3;

        button.y=13;

        button.x= i*button.width;

        [buttonsetBackgroundImage:[UIImage imageNamed:@"red"] forState:UIControlStateNormal];

        [buttonsetBackgroundImage:[UIImage imageNamed:@"yellow"] forState:UIControlStateDisabled];

       // button.backgroundColor = [UIColor redColor];

        [buttonsetTitle:titles[i] forState:UIControlStateNormal];

        [buttonlayoutIfNeeded];

        [buttonaddTarget:self action:@selector(clickbtn:) forControlEvents:UIControlEventTouchUpInside];

        [self.viewaddSubview:button];


    }

}

-(void)clickbtn:(UIButton*)btn{

    self.selectedButton.enabled = YES;

    btn.enabled=NO;

    self.selectedButton = btn;

    self.selectBtnTag= btn.tag;

  //  self.selectedButton.backgroundColor = [UIColor yellowColor];

    [self.selectedButton setBackgroundImage:[UIImage imageNamed:@"yellow"] forState:UIControlStateDisabled];


}

@end

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容