gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:39:49 27 4
gpt4 key购买 nike

此文本下方是几行代码,用于设置我的复选框,但我不想将其写出 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];

最佳答案

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

-(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 次。假设您在名为 myViewUIView 中有 5 个按钮。然后像这样调用上面的方法

for(UIButton *btn in myView){
if([btn isKindOfClass:[UIButton class]])
[self setupWithCheckBox:btn];
}

这一切都假设复选框是一个按钮,如果不是,则将代码段中的 UIButton 替换为任何类。但我认为它是一个按钮。

关于ios - 如何增加变量末尾的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32626806/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com