美文网首页
ios中循环创建按钮一个被点击其余不被点击的情况

ios中循环创建按钮一个被点击其余不被点击的情况

作者: summerTa | 来源:发表于2018-12-25 09:39 被阅读0次

@interface ViewController ()

@property(nonatomic,strong)UIButton *button;

@property(nonatomic,strong)UIButton *fBtn;

@property(nonatomic,strong)NSArray *picarray;

@property(nonatomic,strong)NSArray *picxuan;

@property(nonatomic,strong)UIImage *image;

@property(nonatomic,strong)UIButton *selectedBtn;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

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

    CGFloat width = [UIScreen mainScreen].bounds.size.width;

    CGFloatbtnwidth =110;

    CGFloatbtnheight =42;

    CGFloatbtnspace = (width-4*btnwidth)/5;

    _picarray = @[@"shang",@"niuyue",@"zhenZhou",@"Dalian"];

    _picxuan = @[@"shangS",@"niuyueS",@"zhenZhouS",@"DalianS"];

    for(inti=0; i<4; i++) {

        _button= [[UIButtonalloc]init];

        _button.frame=CGRectMake(btnspace+(btnwidth+btnspace)*i,50, btnwidth, btnheight);

        _button.tag= i;

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

        if(_button.tag==0) {

            [_button setImage:[UIImage imageNamed:_picxuan[i]] forState:UIControlStateNormal];

            _selectedBtn = _button;

        }else{

//            _button.backgroundColor = [UIColor colorWithPatternImage:[UIColor colorWithPatternImage:_picarray[i]]];

            //_button.backgroundColor = [UIColor whiteColor];

           // _button.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:_picarray[i]]];

            [_button setImage:[UIImage imageNamed:_picarray[i]] forState:UIControlStateNormal];

        }

      [self.viewaddSubview:_button];

    }

}

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

    if (_selectedBtn) {

        NSString*str = [NSStringstringWithFormat:@"%ld",btn.tag-1];

        if([strisEqualToString:@"-1"]) {

            [_selectedBtn setImage:[UIImage imageNamed:_picarray[0]] forState:UIControlStateNormal];

        }else{

             [_selectedBtn setImage:[UIImage imageNamed:_picarray[btn.tag-1]] forState:UIControlStateNormal];

        }

    }

    _selectedBtn = btn;

    [_selectedBtn setImage:[UIImage imageNamed:_picxuan[btn.tag]] forState:UIControlStateNormal];

}

@end

相关文章

网友评论

      本文标题:ios中循环创建按钮一个被点击其余不被点击的情况

      本文链接:https://www.haomeiwen.com/subject/oguphqtx.html