iphone自动从文本字段读取

iphone自动从文本字段读取,iphone,uitextfield,Iphone,Uitextfield,我创建了一些表格,里面有文本字段,比如iphone的contacts, (灵感来源于UICatalog示例) 所有这些都在工作,但是我如何通过编程读取这些文本字段的值,我看到它们有一个标记,但是我如何读取它们(因为我不能使用IB来实现这一点,因为它们是通过编程创建的,以进入表中)(这里是noob!) 谢谢 我使用了一些示例hello world样式,我在表格的文本字段中键入,然后单击按钮,键入的文本进入标签, 此示例最终将从表textfields填充我的coredata数据库 正如我说的,我准备

我创建了一些表格,里面有文本字段,比如iphone的contacts, (灵感来源于UICatalog示例)

所有这些都在工作,但是我如何通过编程读取这些文本字段的值,我看到它们有一个标记,但是我如何读取它们(因为我不能使用IB来实现这一点,因为它们是通过编程创建的,以进入表中)(这里是noob!)

谢谢

我使用了一些示例hello world样式,我在表格的文本字段中键入,然后单击按钮,键入的文本进入标签, 此示例最终将从表textfields填充我的coredata数据库 正如我说的,我准备好了,所以请陪我走

因此,当用户点击submit时,我会将文本发送到标签(很快就会发送到coredata)

这是我的文本字段的代码

- (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;
   }

根据您的设置,您可能希望将self.view替换为表。

创建文本字段时,请将对它们的引用存储在实例变量中,例如:

UITextField *textField = ...
self.myTextField = textField;
[theSuperview addSubview:self.myTextField];
然后访问实例上的文本属性

NSString *theString = self.myTextField.text;
@Mako从这里得到帮助 这里appDelegate是应用程序委托类的对象

//自定义表格视图单元格的外观

- (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;
    }
希望你能理解…祝你好运

UITextField *textField = ...
self.myTextField = textField;
[theSuperview addSubview:self.myTextField];
NSString *theString = self.myTextField.text;
- (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;
    }