2009-08-11 66 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委託方法。