objective-c 如何通过标签从 UIView 获取某个子视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24530459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a certain subview from UIView by tag
提问by user3797431
I'm a noob in Objective-C and I have one question.
我是 Objective-C 的菜鸟,我有一个问题。
I have one UILabelobject that I adding to one UIView with this code:
我有一个UILabel对象,可以使用以下代码添加到一个 UIView 中:
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];
Now I want get one subview in my view where this subview has tag = 1and save it on another object like this:
现在我想在我的视图中获取一个子视图,其中该子视图具有tag = 1并将其保存在另一个对象上,如下所示:
UILabel *tagLabel;
tagLabel = // I want get one subview in view where tag = 1
Please help me understand how to do this.
请帮助我了解如何执行此操作。
回答by or azran
Example with UILabel:
示例UILabel:
UILabel *label = (UILabel *)[self.view viewWithTag:1];
good luck!
祝你好运!
回答by Hiren
You can get your subviews with for loop iteration
您可以使用 for 循环迭代获取子视图
for (UIView *i in self.view.subviews){
if([i isKindOfClass:[UILabel class]]){
UILabel *newLbl = (UILabel *)i;
if(newLbl.tag == 1){
/// Write your code
}
}
}
Swift
迅速
let label:UILabel = self.view.viewWithTag(1) as! UILabel
回答by Rameswar Prasad
You can get the subview with the code which others have mentioned, just like
您可以使用其他人提到的代码获取子视图,就像
UILabel *tagLabel = (UILabel*)[view viewWithTag:1];
But an important point to remember,
但要记住的重要一点,
- Make sure the parent view doesn't have the same tag value as of the subview. Otherwise the
viewWithTag:method will return the receiver view (on which you are callingviewWithTag:) instead of returning the actual subview you want.
- 确保父视图与子视图的标签值不同。否则,该
viewWithTag:方法将返回接收者视图(您正在调用viewWithTag:的视图)而不是返回您想要的实际子视图。
So keep the parent view and child views tags distinct whenever you need to use viewWithTag:.
因此,无论何时需要使用viewWithTag:.
回答by Koushik
Swift 3.0 and Swift 4.0
Swift 3.0 和 Swift 4.0
if let subLabel:UILabel = primaryView.viewWithTag(123) as? UILabel {
subLabel.text = "do things here :-D"
}
回答by dasdom
You could use the viewWithTag:method.
您可以使用viewWithTag:方法。
回答by Fantini
If you are on the same view
如果您在同一视图中
UILabel *tagLabel = (UILabel*)[view viewWithTag:1];
Also, if you want a new instance of UILabel
另外,如果你想要一个新的 UILabel 实例
UILabel *newTagLabel = [tagLabel copy];
//customize new label here...
[view addSubView:newTagLabel];

