[英]Can't get values from text fields from UITableView
I have a UITableView
that I used for username/password enter. 我有一个用于输入用户名/密码的
UITableView
。 But I have problem to get values when I click on button. 但是当我单击按钮时,我很难获取值。 This is how I make table:
这是我制作表格的方式:
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
if ([indexPath section] == 0) {
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if ([indexPath row] == 0) {
playerTextField.placeholder = @"example@gmail.com";
playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
playerTextField.returnKeyType = UIReturnKeyNext;
}
else {
playerTextField.placeholder = @"Required";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyDone;
playerTextField.secureTextEntry = YES;
}
playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
playerTextField.textAlignment = NSTextAlignmentLeft;
playerTextField.tag = 0;
playerTextField.clearButtonMode = UITextFieldViewModeNever;
[playerTextField setEnabled: YES];
playerTextField.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell addSubview:playerTextField];
}
}
if ([indexPath section] == 0) { // Email & Password Section
if ([indexPath row] == 0) { // Email
cell.textLabel.text = @"Email";
}
else {
cell.textLabel.text = @"Password";
}
}
return cell;
}
And this is what I have tried in button tap: 这是我在按钮点击中尝试的方法:
...
for (UITableViewCell* aCell in [self.tableView visibleCells] ) {
UITextField* aField = (UITextField *)[aCell viewWithTag:0];
NSLog(aField.text);
}
..
This prints me just: Email Password (labels not data that I entered). 这仅向我显示:电子邮件密码(不标记我输入的数据)。 How to get entered values here?
如何在这里获取输入值?
All views default their tag
to 0. Because of this, [aCell viewWithTag: 0]
is returning the first view with tag zero - in this case the cell's textLabel
- which is a UILabel
that also responds to .text
. 所有视图都将其
tag
默认tag
为0。因此, [aCell viewWithTag: 0]
返回的第一个标签为0的视图-在本例中为单元格的textLabel
这也是一个UILabel
,它也响应.text
。 If you set the tags in your cellForRowAtIndexPath:
to something other than zero it should start working for you. 如果您将
cellForRowAtIndexPath:
的标记设置为非零值,它将开始为您工作。
In that case you can give the tag to textfield like 在这种情况下,您可以将标签提供给文本字段,例如
playerTextField.tag = 115 playerTextField.tag = 115
and you can get view by tag like 你可以通过标签来查看
UITextField *aField = (UITextField *)[aCell viewWithTag:115]; UITextField * aField =(UITextField *)[aCell viewWithTag:115];
and display NSLog(@"text:%@",aField.text); 并显示NSLog(@“ text:%@”,aField.text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.