如何通过标签从 UIView 获取某个子视图 [英] How to get a certain subview from UIView by tag

查看:79
本文介绍了如何通过标签从 UIView 获取某个子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Objective-C 的菜鸟,我有一个问题.

I'm a noob in Objective-C and I have one question.

我有一个 UILabel 对象,我使用以下代码将其添加到一个 UIView 中:

I have one UILabel object that I adding to one UIView with this code:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,10,self.view.frame.size.width-15-70, 30)];
label.tag = 1;
label.font = [PublicObject fontTexts:17];
label.textAlignment = NSTextAlignmentRight;
label.textColor = [UIColor whiteColor];
[cell setBackgroundColor:[UIColor clearColor]];

 UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
 view.backgroundColor = [PublicObject colorWithHexString:@"cd4110"];
 label.text = [filterData objectAtIndex:indexPath.row];
 view addSubview:label];

现在我想在我的视图中获得一个子视图,其中该子视图具有 tag = 1 并将其保存在另一个对象上,如下所示:

Now I want get one subview in my view where this subview has tag = 1 and save it on another object like this:

UILabel *tagLabel;
tagLabel = // I want get one subview in view where tag = 1 

请帮助我了解如何执行此操作.

Please help me understand how to do this.

推荐答案

您可以通过 for 循环迭代获取子视图

You can get your subviews with for loop iteration

for (UIView *i in self.view.subviews){
      if([i isKindOfClass:[UILabel class]]){
            UILabel *newLbl = (UILabel *)i;
            if(newLbl.tag == 1){
                /// Write your code
            }
      }
}

迅捷

let label:UILabel = self.view.viewWithTag(1) as! UILabel

这篇关于如何通过标签从 UIView 获取某个子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆