在UIView中查找类型的所有控件 [英] Find all controls of a type in a UIView
问题描述
我正在寻找一种自动本地化按钮/文本字段等上的文本的方法,对于这种方法,我需要在UIView上查找所有(例如)UIButton。
我尝试了以下2个方法,但他们都没有像我想让他们工作的工作:
for(UIView * subView in self.view.subviews)
{
NSLog(@object class:%@,[subView class]);
if([subView isMemberOfClass:[UIButton class]])
NSLog(@Button found!);
}
这段代码的问题是RoundedRectButton不匹配UIButton
我也试过下面的:
code> for(UIButton * button in self.view.subviews)
{
//做我的东西
}
$但是愚蠢的事情是,可可触摸实际上只是列出所有的子视图在for循环(也是UITextFields等)。
有没有办法实际上只是从一个视图获取所有UIButtons?或者我真的需要通过查看他们的选择器找到控件。
解决方案第一种方法是正确的, isMemberOfClass函数 isKindOfClass :
isKindOfClass :返回布尔值
,表示接收者是
是给定类的实例还是任何继承了该类中
的类的
实例。
I am looking for a way to automatically localize texts on buttons/textfields etc and for this method I need to find all (for example) UIButton's on a UIView.
I tried the following 2 methods, but they both do no work like I want them to work:
for (UIView* subView in self.view.subviews) { NSLog(@"object class : %@", [subView class]); if ([subView isMemberOfClass:[UIButton class]]) NSLog(@"Button found!"); }
The problem with this piece of code is that a RoundedRectButton does not match the UIButton class, while it really is just a UIButton.
I also tried the following:
for (UIButton* button in self.view.subviews) { // Do my stuff }
But the stupid thing is, is that cocoa-touch actually just lists all subviews in that for-loop (also the UITextFields etc).
Is there a way to actually just get all UIButtons from a view? Or do I really need to find controls by looking at their selectors.
解决方案the first method is correct, except you need to change isMemberOfClass function to isKindOfClass:
isKindOfClass: Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.
这篇关于在UIView中查找类型的所有控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!