我可以通过iPhone SDK中的标签获取元素吗?
答
使用viewWithTag
方法。例如如果你的按钮是在控制器的观点和你的按钮的标签是100下面一行将返回按钮:
UIButton *button = (UIButton *)[self.view viewWithTag:100];
编辑:与特定标签
获取视图斯威夫特 -
let view = self.view.viewWithTag(100)
如果你想确保你有特定类型的视图,比如UIButton,你应该检查类型:
if let button = self.view.viewWithTag(100) as? UIButton {
//Your code
}
答
UIButton *button=(UIButton *)[self.view viewWithTag:tag];
//现在你可以得到基于标签值的按钮
答
//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);