2009-08-11 64 views
0

在联系人应用程序的添加/编辑视图中,如果您在字段中点击“返回”键,则循环访问每个UITextFields。这些字段似乎在UITableViewCell中。什么是这样做的好方法?在UITableViewCell内的UITextFields之间导航

当我有一系列不在表格内的UITextField时,我可以调用 [self.view viewWithTag:tag]来获取视图列表以及循环使用的视图。但是在一个表格中,我只能看到一个视图,而我不知道如何将其转换。

-(BOOL)textFieldShouldReturn:(UITextField *)textField { 
    // Find the next entry field 
    for (UIView *view in [self entryFields]) { 
     if (view.tag == (textField.tag + 1)) { 
      [view becomeFirstResponder]; 
      break; 
     } 
    } 

    return NO; 
} 

/* 
Returns an array of all data entry fields in the view. 
Fields are ordered by tag, and only fields with tag > 0 are included. 
Returned fields are guaranteed to be a subclass of UIResponder. 

From: http://iphoneincubator.com/blog/tag/uitextfield 
*/ 
- (NSArray *)entryFields { 
    if (!entryFields) { 
     self.entryFields = [[NSMutableArray alloc] init]; 
     NSInteger tag = 1; 
     UIView *aView; 
     while (aView = [self.view viewWithTag:tag]) { 
      if (aView && [[aView class] isSubclassOfClass:[UIResponder class]]) { 
       [entryFields addObject:aView]; 
      } 
      tag++; 
     } 
    } 
    return entryFields; 
} 

回答

0

刚刚发现这一点,同时浏览自己的东西 - 抱歉的延迟。

如果您尚未解决该问题,则可以按照屏幕上显示的相同顺序创建一个uitextfield对象数组。在创建表格时,设置文本字段的.tag属性。在文本字段委托方法中 - 读取标签,加1(移至下一个字段),并发出becomefirstresponder调用。

你也可以看看textFieldDidEndEditing委托方法。