iPhone以编程方式从文本字段读取

发布于 2024-10-17 17:50:42 字数 2938 浏览 1 评论 0原文

我创建了一些表格,里面有文本字段,就像 iPhone 的联系人一样, (受到 UICatalog 示例的启发)

一切都正常,但是我如何以编程方式读取此文本字段的值,我看到它们有一个标签,但我如何从中读取它们(因为在创建它们时我无法使用 IB以编程方式进入表格)(这里是菜鸟!)

谢谢,

我使用一些示例 hello world 样式,我在表格的文本字段上键入,然后单击按钮,键入的文本将转到标签, 此示例最终是从表文本字段填充我的 coredata 数据库 正如我所说,我对此很菜鸟,所以请引导我完成

,以便当用户点击“提交”时,我将文本发送到标签(很快就会发送到 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;
}

I have created some tables with text fields inside, like in the contacts for the iphone,
(inspired from UICatalog example)

all is working, but how can I programatically read the values of this text fields, I see they have a tag, but how can I read from them (because I cannot use the IB for this as they were created programatically to go inside the tables)(noob here!)

thanks,

Im using some example hello world style where I type on the text field of the table, then click on button and the text typed goes to label,
This sample is to ultimately populate my coredata db from the table textfields
As I said im preatty noob for this so please walk me trough

so when user hit submit, I get the text to the label (and soon to coredata)

 - (IBAction) submitYourName;{
lblUserTypedName.text = txtUserName.text;
NSLog(@"received");
 }

this is the code for my textfield

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

and this to show that text field in the table cell

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吹梦到西洲 2024-10-24 17:50:42

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

希望您理解...祝您好运!

@Mako take help from here
Here appDelegate is the object of your Application Delegate class

// Customize the appearance of table view cells.

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

Hope you understand...Good Luck!

吃兔兔 2024-10-24 17:50:42
UITextField *textField = (UITextField *)[self.view viewWithTag:100];

根据您的设置,您可能需要将 self.view 替换为您的表格。

UITextField *textField = (UITextField *)[self.view viewWithTag:100];

Depending on your setup, you might want to replace self.view with your table.

落花浅忆 2024-10-24 17:50:42

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

UITextField *textField = ...
self.myTextField = textField;
[theSuperview addSubview:self.myTextField];

然后访问实例上的文本属性

NSString *theString = self.myTextField.text;

When you create the text fields, store a reference to them in an instance variable something like:

UITextField *textField = ...
self.myTextField = textField;
[theSuperview addSubview:self.myTextField];

Then access the text property on your instance

NSString *theString = self.myTextField.text;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文