for循环创建button问题

西湖听雨1990 发布于 2017/11/16 11:40
阅读 126
收藏 0

for循环创建button。

定义的

/**        **/

@property (nonatomic, strong)  UIButton * selectBtn;

/**        **/

@property (nonatomic, strong)  NSArray * typeArray;

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

    

    _typeArray = [NSArray arrayWithObjects:@"闪退",@"页面反应慢",@"数据错误",@"登录注册问题",@"提现问题",@"其它问题", nil];

    for (int i = 0; i < _typeArray.count; i++)

    {

        int a = i/3;   //取余

        int b = i%3;   //取整

        NSLog(@"b:%d==a:%d",b,a);

        UIButton *tagButton = [[UIButton alloc] initWithFrame:CGRectMake(10+(10+(UISCREENWIDTH-40)/3)*b, 90+(30+10)*a, (UISCREENWIDTH-40)/3, 30)];

        tagButton.layer.borderColor = [UIColor lightGrayColor].CGColor;

        tagButton.tag = i;

        tagButton.titleLabel.font = [UIFont systemFontOfSize:15];

        [tagButton setTitle:_typeArray[i] forState:UIControlStateNormal];

        [tagButton setBackgroundImage:[UIImage imageNamed:@"bg"] forState:UIControlStateNormal];

        [tagButton setBackgroundImage:[UIImage imageNamed:@"select_bg"] forState:UIControlStateSelected];

        tagButton.backgroundColor = [UIColor orangeColor];

        [tagButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

        [tagButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

        

        [tagButton addTarget:self action:@selector(tagButtonAction:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:tagButton];

        

        self.selectBtn = tagButton;

    }

    

    

}

- (void)tagButtonAction:(UIButton *)sender{

    

        sender.selected = !sender.selected;

    

        if (_selectBtn == sender) {

            _selectBtn.selected = YES;

            sender.selected = NO;

    

        }else{

            sender.selected = YES;

            _selectBtn.selected = NO;

          

        }

    _selectBtn = sender;

    NSLog(@"--:%ld===:%i",(long)sender.tag,sender.selected);

}

 

现在出现的问题是:每第一次运行之后,点击最后一个按钮不能选中,点击其它的任何一个按钮之后,在点最后一个按钮就可以正常选中了,请问一下 这个是怎么回事的了?

万分感谢!!!!

加载中
返回顶部
顶部