Я думаю, что ОП пытается понять, как получить доступ к значению UITextField после того, как пользователь ввел данные в каждом поле. Это будет недоступно в момент создания ячеек, как было предложено @willcodejavaforfood.
Я реализую форму и стараюсь сделать ее максимально удобной для пользователя. Это выполнимо, но имейте в виду, что он может быть запутанным в зависимости от количества UITableViewCells/UITextFields, которое у вас есть.
Во-первых, на ваш вопрос повторно: доступ к ценности UITextField:
1) Сделайте ваш контроллер вид <UITextFieldDelegate>
2) осуществлять следующий метод:
- (void) textFieldDidEndEditing:(UITextField *)textField {
NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell*)[[textField superview] superview]]; // this should return you your current indexPath
// From here on you can (switch) your indexPath.section or indexPath.row
// as appropriate to get the textValue and assign it to a variable, for instance:
if (indexPath.section == kMandatorySection) {
if (indexPath.row == kEmailField) self.emailFieldValue = textField.text;
if (indexPath.row == kPasswordField) self.passwordFieldValue = textField.text;
if (indexPath.row == kPasswordConfirmField) self.passwordConfirmFieldValue = textField.text;
}
else if (indexPath.section == kOptionalSection) {
if (indexPath.row == kFirstNameField) self.firstNameFieldValue = textField.text;
if (indexPath.row == kLastNameField) self.lastNameFieldValue = textField.text;
if (indexPath.row == kPostcodeField) self.postcodeFieldValue = textField.text;
}
}
Я также использовать аналогичный синтаксис, чтобы убедиться, что текущее редактируемое поле видимо:
- (void) textFieldDidBeginEditing:(UITextField *)textField {
CustomCell *cell = (CustomCell*) [[textField superview] superview];
[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
И, наконец, вы можете обрабатывать textViewShouldReturn:
подобным образом:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell*)[[textField superview] superview]];
switch (indexPath.section) {
case kMandatorySection:
{
// I am testing to see if this is NOT the last field of my first section
// If not, find the next UITextField and make it firstResponder if the user
// presses ENTER on the keyboard
if (indexPath.row < kPasswordConfirmField) {
NSIndexPath *sibling = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
CustomCell *cell = (CustomCell*)[self.tableView cellForRowAtIndexPath:sibling];
[cell.cellTextField becomeFirstResponder];
} else {
// In case this is my last section row, when the user presses ENTER,
// I move the focus to the first row in next section
NSIndexPath *sibling = [NSIndexPath indexPathForRow:kFirstNameField inSection:kOptionalSection];
MemberLoginCell *cell = (MemberLoginCell*)[self.memberTableView cellForRowAtIndexPath:sibling];
[cell.cellTextField becomeFirstResponder];
}
break;
}
...
}
Cheers Rog, я попробую это, когда вернусь к своему mac. Интересно, как Apple это делает, как приложение «Настройки», я бы подумал, что буду немного более прямолинейным/более чистым, чем это. Я полагаю, что они, вероятно, делают это совершенно по-другому, и все с кодом тоже. – Darthtong 2010-12-07 12:18:41