我可以从 iOS 中通过 Tag 获取元素吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3657419/
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
Can I get the element by Tag from iOS?
提问by DNB5brims
I want to get back a button, I've already assign the tag to there, how can I do so?
我想取回一个按钮,我已经将标签分配给了那里,我该怎么做?
回答by Swapnil Luktuke
Use the viewWithTag
method. e.g. if your button is in your controller's view and your button's tag is 100 the following line will return the button:
使用viewWithTag
方法。例如,如果您的按钮在您的控制器视图中并且您的按钮标签为 100,则以下行将返回该按钮:
UIButton *button = (UIButton *)[self.view viewWithTag:100];
EDIT:
编辑:
Get view with particular tag in Swift -
在 Swift 中使用特定标签获取视图 -
let view = self.view.viewWithTag(100)
If you want to make sure you have the specific type of view, say a UIButton, you should check the type:
如果你想确保你有特定类型的视图,比如 UIButton,你应该检查类型:
if let button = self.view.viewWithTag(100) as? UIButton {
//Your code
}
回答by vntstudy
UIButton *button=(UIButton *)[self.view viewWithTag:tag];
//Now you can get your button based on tag value
//现在您可以根据标签值获取您的按钮
回答by TEJAS PATELIA
//Get View using tag
UITextField *textFieldInView = (UITextField*)[self.view viewWithTag:sender.tag+1000];
UILabel *labelInView = (UILabel*)[self.view viewWithTag:sender.tag+2000];
//Print its content based on the tags
NSLog(@"The tag is %d ", textFieldInView.tag);
NSLog(@"The tag is %d ", labelInView.tag);
NSLog(@"The Content is %@ ", textFieldInView.text);
NSLog(@"The Content is %@ ", labelInView.text);