我可以通过iOS的Tag获取元素吗? [英] Can I get the element by Tag from iOS?

查看:179
本文介绍了我可以通过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屋!

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