我可以从 iOS 中通过 Tag 获取元素吗? [英] Can I get the element by Tag from iOS?
本文介绍了我可以从 iOS 中通过 Tag 获取元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想取回一个按钮,我已经将标签分配给了那里,我该怎么做?
I want to get back a button, I've already assign the tag to there, how can I do so?
推荐答案
使用 viewWithTag
方法.例如如果您的按钮在控制器的视图中并且您的按钮标签为 100,则以下行将返回该按钮:
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:
UIButton *button = (UIButton *)[self.view viewWithTag:100];
在 Swift 中获取带有特定标签的视图 -
Get view with particular tag in Swift -
let view = self.view.viewWithTag(100)
如果你想确保你有特定类型的视图,比如 UIButton,你应该检查类型:
If you want to make sure you have the specific type of view, say a UIButton, you should check the type:
if let button = self.view.viewWithTag(100) as? UIButton {
//Your code
}
这篇关于我可以从 iOS 中通过 Tag 获取元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文