我可以通过iPhone SDK中的标签获取元素吗? [英] Can I get the element by Tag from iPhone SDK?

查看:101
本文介绍了我可以通过iPhone SDK中的标签获取元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想取回一个按钮,我已经将标签分配到那里,我该怎么办?谢谢。

I want to get back a button, I've already assign the tag to there, how can I do so? Thank you.

推荐答案

使用 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
}

这篇关于我可以通过iPhone SDK中的标签获取元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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