我已经在里面创建了一些带有文本字段的表,比如在iphone的联系人中, (灵感来自UICatalog示例)
一切正常,但是我怎么能以编程方式读取这些文本字段的值,我看到它们有一个标签,但我怎么能从它们中读取(因为我不能使用IB来实现它们,因为它们是以编程方式创建的在桌子里面)(noob在这里!)
感谢,
我使用了一些示例hello world样式,我在表格的文本字段中键入,然后单击按钮,键入的文本转到标签, 此示例最终将从表textfields填充我的coredata db 正如我所说的那样,我是非常好的noob,所以请带我走吧
因此,当用户点击提交时,我会将文本提交给标签(很快就会出现coredata)
- (IBAction) submitYourName;{
lblUserTypedName.text = txtUserName.text;
NSLog(@"received");
}
这是我的文本域的代码
- (UITextField *)textFieldNormal
{
if (textFieldNormal == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldNormal = [[UITextField alloc] initWithFrame:frame];
textFieldNormal.borderStyle = UITextBorderStyleRoundedRect;
textFieldNormal.textColor = [UIColor blackColor];
textFieldNormal.font = [UIFont systemFontOfSize:17.0];
textFieldNormal.placeholder = @"<enter text>";
textFieldNormal.backgroundColor = [UIColor whiteColor];
textFieldNormal.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textFieldNormal.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)
textFieldNormal.returnKeyType = UIReturnKeyDone;
textFieldNormal.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldNormal.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textFieldNormal.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textFieldNormal setAccessibilityLabel:NSLocalizedString(@"NormalTextField", @"")];
}
return textFieldNormal;
}
这将显示表格单元格中的文本字段
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
static NSString *kCellTextField_ID = @"CellTextField_ID";
cell = [tableView dequeueReusableCellWithIdentifier:kCellTextField_ID];
if (cell == nil)
{
// a new cell needs to be created
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellTextField_ID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
// a cell is being recycled, remove the old edit field (if it contains one of our tagged edit fields)
UIView *viewToCheck = nil;
viewToCheck = [cell.contentView viewWithTag:kViewTag];
if (viewToCheck)
[viewToCheck removeFromSuperview];
}
UITextField *textField = [[self.dataSourceArray objectAtIndex: indexPath.section] valueForKey:kViewKey];
[cell.contentView addSubview:textField];
return cell;
}
答案 0 :(得分:0)
UITextField *textField = (UITextField *)[self.view viewWithTag:100];
根据您的设置,您可能希望将 self.view 替换为您的表格。
答案 1 :(得分:0)
创建文本字段时,请在实例变量中存储对它们的引用,如:
UITextField *textField = ...
self.myTextField = textField;
[theSuperview addSubview:self.myTextField];
然后访问您实例上的text属性
NSString *theString = self.myTextField.text;
答案 2 :(得分:0)
@Mako从这里获得帮助 这里appDelegate是Application Delegate类的对象
//自定义表格视图单元格的外观。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.fieldTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
}
// Configure the cell.
UITextField *txtTemp = (UITextField *)[cell.contentView viewWithTag:1];
UITextField *txtTemp1 = (UITextField *)[cell.contentView viewWithTag:2];
UITextField *txtTemp2 = (UITextField *)[cell.contentView viewWithTag:3];
UITextField *txtTemp3 = (UITextField *)[cell.contentView viewWithTag:4];
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
txtTemp1.placeholder = @"H/No";
appDelegate.HouseNo = [NSString stringWithFormat:@"%@",txtTemp1.text];
NSLog(@"%@",appDelegate.HouseNo);
break;
case 1:
txtTemp1.placeholder = @"Street";
appDelegate.street = [NSString stringWithFormat:@"%@",txtTemp1.text];
NSLog(@"%@", appDelegate.street);
break;
case 2:
txtTemp.placeholder = @"City";
txtTemp2.placeholder = @"State";
txtTemp3.placeholder = @"Zip Code";
appDelegate.city = [NSString stringWithFormat:@"%@",txtTemp.text];
NSLog(@"%@",appDelegate.city);
appDelegate.state = [NSString stringWithFormat:@"%@",txtTemp2.text];
NSLog(@"%@",appDelegate.state);
appDelegate.zipCode = [NSString stringWithFormat:@"%@",txtTemp3.text];
NSLog(@"%@",appDelegate.zipCode);
break;
default:
break;
}
break;
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath {
CGRect cellRectangle = CGRectMake (0, 10, 300, 70);
CGRect Field1Frame = CGRectMake (10, 10, 290, 70);
CGRect Field2Frame = CGRectMake (10, 10, 90, 70);
CGRect Field3Frame = CGRectMake (110, 10, 95, 70);
CGRect Field4Frame = CGRectMake (220, 10, 85, 70);
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:cellRectangle reuseIdentifier:identifier] autorelease];
UITextField *textField;
UITextField *textField1;
UITextField *textField2;
UITextField *textField3;
//Initialize Label with tag 1.
textField = [[UITextField alloc] initWithFrame:Field2Frame];
textField.tag = 1;
[cell.contentView addSubview:textField];
//Initialize Label with tag 2.
textField1 = [[UITextField alloc] initWithFrame:Field1Frame];
textField1.tag = 2;
[cell.contentView addSubview:textField1];
//Initialize Label with tag 3.
textField2 = [[UITextField alloc] initWithFrame:Field3Frame];
textField2.tag = 3;
[cell.contentView addSubview:textField2];
//Initialize Label with tag 4.
textField3 = [[UITextField alloc] initWithFrame:Field4Frame];
textField3.keyboardType = UIKeyboardTypeNumberPad;
textField3.tag = 4;
[cell.contentView addSubview:textField3];
[textField release];
[textField1 release];
[textField2 release];
[textField3 release];
return cell;
}
希望你明白......祝你好运!