iOS tag的使用

一、添加标记 (标记不能为0)

UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,0,100,100)];
backBtn.backgroundColor = [UIColor blackColor];
backBtn.tag = 666;
[self.view addSubview:backBtn];

二、取出控件

// 方法一
for (UIButton *find_Btn in self.view.subviews) {
        
    if (find_Btn.tag == 666){
        find_Btn.backgroundColor = [UIColor whiteColor];
    }
    if (find_Btn.tag == 999){
        find_Btn.backgroundColor = [UIColor greenColor];
    }
        
 }
// 方法二
UIButton *find_Btn = (UIButton*)[self.view viewWithTag:666]; 
find_Btn.backgroundColor
= [UIColor redColor];
原文地址:https://www.cnblogs.com/xsphehe/p/5832169.html