获取所有UITextField的数组

问题描述:

如何在视图控制器中获取所有UITextField的数组?获取所有UITextField的数组

编辑:我不想将textfields硬编码到数组中。我实际上想要从该委托的调用者的所有字段的委托内获取列表。

+0

创建它们时将它们添加到数组??? – 2012-01-13 22:25:00

+0

请注意,委托不包含对文本字段的引用,这是相反的方式,无法单独从委托中查找字段。 http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html – Tim 2012-01-17 19:46:52

递归执行搜索所有子视图子视图:(此你抓住嵌入在uiscrollview中的textfields的方式,等等)

-(NSArray*)findAllTextFieldsInView:(UIView*)view{ 
    NSMutableArray* textfieldarray = [[[NSMutableArray alloc] init] autorelease]; 
    for(id x in [view subviews]){ 
     if([x isKindOfClass:[UITextField class]]) 
      [textfieldarray addObject:x]; 

     if([x respondsToSelector:@selector(subviews)]){ 
      // if it has subviews, loop through those, too 
      [textfieldarray addObjectsFromArray:[self findAllTextFieldsInView:x]]; 
     } 
    } 
    return textfieldarray; 
} 

-(void)myMethod{ 
    NSArray* allTextFields = [self findAllTextFieldsInView:[self view]]; 
    // ... 
} 
+0

这不是'if([x respondsToSelector:@selector(subviews)]){''冗余为'-subviews'返回'@property(非原子,只读,复制)NSArray * subviews'。因此,该数组中的任何项目将是'UIView'或一个子类或'UIView'类型,它响应'@selector(subviews)' – 2012-01-13 23:43:55

+0

我只是很不稳定,知道类 - 如果某些骨头以'subviews'包含非'UIView'-继承的对象,并因此不响应'@selector(subviews)'的方式来区分'UIView'会怎么样? – Tim 2012-01-13 23:59:20

+0

重写该方法并添加非'UIView'没有意义 - 如果用户确实很聪明,他们很可能知道他们为什么要这么做,以及它可能涉及哪些风险。至少这是人们希望的;) – 2012-01-14 00:20:59

您可以循环控制器视图的子视图。

这里有一个粗糙例如:

NSMutableArray *arrOfTextFields = [NSMutableArray array]; 
for (id subView in self.view.subviews) 
{ 
    if ([subView isKindOfClass:[UITextField class]]) 
     [arrOfTextFields addObject:subView]; 
} 
+0

这不会捕获其他视图中嵌入的UITextFields。 – 2012-01-13 22:13:42

编辑递归没有全局变量(只为添)

-(NSArray*)performUIElementSearchForClass:(Class)targetClass onView:(UIView*)root{ 

    NSMutableArray *searchCollection = [[[NSMutableArray alloc] init] autorelease]; 

    for(UIView *subview in root.subviews){ 

     if ([subView isKindOfClass:targetClass]) 
      [searchCollection addObject:subview]; 

     if(subview.subviews.count > 0) 
      [searchCollection addObjectsFromArray:[self performUIElementSearchForClass:targetClass onView:subview]]; 

    } 

    return searchCollection; 
} 
+0

ns * muttable *数组 – Tim 2012-01-13 22:07:39

+0

感谢蒂姆,没有自动更正在SO ;-) – Cyprian 2012-01-13 22:09:42

+0

我发现自己经常在SO:'NSMutableArray] alloc] init] autorelease'上键入这样的内容:P – Tim 2012-01-13 22:10:25

如果你知道你需要一个NSArray包含所有UITextField的那么为什么不把它们添加到数组?

NSMutableArray *textFields = [[NSMutableArray alloc] init]; 

UITextField *textField = [[UITextField alloc] initWithFrame:myFrame]; 

[textFields addObject:textField]; // <- repeat for each UITextField 

如果您使用的是笔尖然后使用IBOutletCollection

@property (nonatomic, retain) IBOutletCollection(UITextField) NSArray *textFields; 

然后所有的UITextField的连接到阵列

+0

将它们硬编码为数组毫无意义...打败了我希望将所有文本字段数组作为我可以手动创建它... – Bot 2012-01-17 16:17:29

+0

如果在创建时将“UITextField”添加到“NSMutableArray”中,则没有硬编码 – 2012-01-17 17:04:26

+0

我将根据来自API的数据创建文本字段,所以我赢了不知道哪些文本字段在那里。我可以在创建它们时以编程方式添加它们,但我更愿意只抓取已存在的字段列表,而不是根据开发人员将每个字段添加到数组中。 – Bot 2012-01-17 17:20:42

- 使用下面的代码获取数组,其中包含的文本值所有显示在视图上的UITextField:

NSMutableArray *addressArray=[[NSMutableArray alloc] init]; 

    for(id aSubView in [self.view subviews]) 
    { 
      if([aSubView isKindOfClass:[UITextField class]]) 
      { 
        UITextField *textField=(UITextField*)aSubView; 
        [addressArray addObject:textField.text]; 
      } 
    } 
    NSLog(@"%@", addressArray);