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

#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

你可能感兴趣的:(按钮的选中与非选中的状态的切换)