如何增加变量末尾的数字?

时间:2015-09-17 09:25:10

标签: ios objective-c

在此文本下面是几行代码,用于设置我的复选框,但是我不想写出5次。

复选框是一个按钮,当我点击它时会更改它的标题标签。我通过重用函数实现了这一点。

但是我仍然很好奇是否可以使用for循环来增加变量名末尾的数字?

 [[checkbox layer]setBorderColor:[[UIColor blackColor]CGColor]];
 [[checkbox layer]setBorderWidth:0.1];
 [[checkbox layer]setCornerRadius:5];
 UITapGestureRecognizer *tapGesture =
 [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(check)];
 [checkbox addGestureRecognizer:tapGesture];

2 个答案:

答案 0 :(得分:1)

您可以像这样编写一个单独的方法

lst <- lapply(df, function(x) if(is.numeric(x)) 
        seq_along(x)[!(abs((x-mean(x))/sd(x))>2)] else seq_along(x))

df[Reduce(intersect,lst),]

然后使用您想要的不同按钮调用该方法5次。假设-(void)setupWithCheckBox:(UIButton *)button{ //this is assuming checkbox is a button [[button layer]setBorderColor:[[UIColor blackColor]CGColor]]; [[button layer]setBorderWidth:0.1]; [[button layer]setCornerRadius:5]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(check)]; [button addGestureRecognizer:tapGesture]; } 内有5个按钮,名为UIView。然后像这样调用上面的方法

myView

这都是假设复选框是一个按钮,如果它不在片段中的替代for(UIButton *btn in myView){ if([btn isKindOfClass:[UIButton class]]) [self setupWithCheckBox:btn]; } ,那么它是什么类。但我认为这是一个按钮。

答案 1 :(得分:1)

UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(10, 80, 20, 20)];
button.tag=9;
button.backgroundColor=[UIColor redColor];
[self.view addSubview:button];

for (int i=0; i<5; i++){

   UIButton *lastBtn=(UIButton *)[self viewWithTag:9+i];

        CGRect frame=lastImg.frame;
        frame.origin.x+=frame.size.width+8;

        UIButton *btn=[[UIButton alloc]initWithFrame:frame];

        btn.tag=9+i+1;
        [self.view addSubview:btn];

        btn.translatesAutoresizingMaskIntoConstraints=NO;


        NSDictionary *viewsDictionary = @{@"btn":btn,@"lastBtn":lastBtn};


        [_view_back addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[lastBtn]-8-[btn]"
                                                                          options:0
                                                                          metrics:nil
                                                                            views:viewsDictionary]];

        NSLayoutConstraint *width =[NSLayoutConstraint
                                    constraintWithItem:btn
                                    attribute:NSLayoutAttributeWidth
                                    relatedBy:0
                                    toItem:lastImg
                                    attribute:NSLayoutAttributeWidth
                                    multiplier:1.0
                                    constant:0];
        NSLayoutConstraint *height =[NSLayoutConstraint
                                     constraintWithItem:btn
                                     attribute:NSLayoutAttributeHeight
                                     relatedBy:0
                                     toItem:lastBtn
                                     attribute:NSLayoutAttributeHeight
                                     multiplier:1.0
                                     constant:0];

        NSLayoutConstraint *top = [NSLayoutConstraint
                                   constraintWithItem:btn
                                   attribute:NSLayoutAttributeTop
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:lastBtn
                                   attribute:NSLayoutAttributeTop
                                   multiplier:1.0f
                                   constant:0.f];

        [_view_back addConstraint:width];
        [_view_back addConstraint:height];
        [_view_back addConstraint:top];

    }