在iPhone中访问动态创建的文本字段的文本?

时间:2012-08-17 13:02:59

标签: iphone uitextfield

我创建了textfield动态地我需要访问在textfield中输入的文本。请帮我这样做。

 -(void)createHHDynamicFieldForYValue:(int )yVal
  {

NSString *lblStr=[[NSString alloc]initWithString:[[healthyHeartFieldsAry objectAtIndex:tagHHCount] objectForKey:@"TestName"]];

dynamicHealthyHeartLbl=[[UILabel alloc] initWithFrame:CGRectMake(50,yVal,278,45)];
dynamicHealthyHeartLbl.backgroundColor=[UIColor clearColor];
dynamicHealthyHeartLbl.textColor=[UIColor colorWithRed:17/255.0 green:61/255.0 blue:83/255.0 alpha:1];
dynamicHealthyHeartLbl.textAlignment = UITextAlignmentLeft;
dynamicHealthyHeartLbl.text=lblStr;
dynamicHealthyHeartLbl.font = [UIFont boldSystemFontOfSize:18];
[healthyHeartScrollView addSubview:dynamicHealthyHeartLbl];
[dynamicHealthyHeartLbl release];
[lblStr release];



dynamicHealthyHeartImgVw=[[UIImageView alloc] initWithFrame:CGRectMake(375, yVal, 278,kTxtImgHgVal)];
dynamicHealthyHeartImgVw.image=[UIImage imageNamed:@"birth_date.png"];
dynamicHealthyHeartImgVw.backgroundColor=[UIColor clearColor];
[healthyHeartScrollView addSubview:dynamicHealthyHeartImgVw];
[dynamicHealthyHeartImgVw release];



dynamicHealthyHeartTxtFld=[[UITextField alloc] initWithFrame:CGRectMake(380,yVal,278,kTxtImgHgVal )];
dynamicHealthyHeartTxtFld.borderStyle = UITextBorderStyleNone;
dynamicHealthyHeartTxtFld.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
dynamicHealthyHeartTxtFld.textAlignment=UITextAlignmentLeft;
dynamicHealthyHeartTxtFld.textColor=[UIColor colorWithRed:17/255.0 green:61/255.0 blue:83/255.0 alpha:1];
dynamicHealthyHeartTxtFld.font = [UIFont systemFontOfSize:18];
dynamicHealthyHeartTxtFld.backgroundColor = [UIColor clearColor];
dynamicHealthyHeartTxtFld.autocorrectionType = UITextAutocorrectionTypeNo;
dynamicHealthyHeartTxtFld.returnKeyType = UIReturnKeyDone;
dynamicHealthyHeartTxtFld.clearButtonMode = UITextFieldViewModeWhileEditing;
dynamicHealthyHeartTxtFld.autocapitalizationType=UITextAutocapitalizationTypeNone;
dynamicHealthyHeartTxtFld.delegate = self;
dynamicHealthyHeartTxtFld.placeholder=[[healthyHeartFieldsAry objectAtIndex:tagHHCount] objectForKey:@"TestName"];
dynamicHealthyHeartTxtFld.tag=tagHHCount;
[healthyHeartScrollView addSubview:dynamicHealthyHeartTxtFld];
[dynamicHealthyHeartTxtFld release];


[healthyHeart_TxtFldAry addObject:dynamicHealthyHeartTxtFld];

[dynamicHealthyHeartTxtFld release];


tagHHCount++;


}

int YVal=35;
tagHHCount=0;
NSLog(@"healthyHeartFieldsAry Count %d",[healthyHeartFieldsAry count]);
for (int i=0; i<[healthyHeartFieldsAry count]; i++)
{
    [self createHHDynamicFieldForYValue:YVal];
    YVal=YVal+80;
}

在TextField中结束编辑

    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
        if(enteredHealthyHeart)
        {
            for(int i=0;i<[healthyHeart_TxtFldAry count];i++)
            {
                if(textField == [healthyHeart_TxtFldAry objectAtIndex:i])
                {
                    getHHTag=textField.tag;
                    //              UITextField *tempTxtFld=[healthyHeart_TxtFldAry objectAtIndex:getHHTag];

                dynamicHealthyHeartTxtFld=[healthyHeart_TxtFldAry objectAtIndex:getHHTag];
                if([dynamicHealthyHeartTxtFld.text length]>0)
                {
                    NSString *tempStr=dynamicHealthyHeartTxtFld.text;
                    NSLog(@"tempStr %@",tempStr);
                    if(![self checkForDecimalValue:tempStr])
                    {
                    Custom_Alert *alert=[[Custom_Alert alloc] initWithTitle:@"Alert" message:@"Please Enter Numeric Values" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert show];
                    [alert release];

                }
                else
                {

                    if(([tempStr intValue] >= [[[healthyHeartFieldsAry objectAtIndex:getHHTag] objectForKey:@"MinVal"] intValue])&&([tempStr intValue] <= [[[healthyHeartFieldsAry objectAtIndex:getHHTag]objectForKey:@"MaxVal"]intValue]))
                    {
                        NSLog(@"validation Succcess!!!");
                    }
                    else
                    {
                        Custom_Alert *alert=[[Custom_Alert alloc] initWithTitle:@"Alert" message:@"Please check the Details" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                        [alert show];
                        [alert release];
                    }


                }
            }


            [textField resignFirstResponder];
            //return YES;

        }
    }

}

}

我想知道如何访问textField文本。如何识别输入的文本字段文本。任何帮助???

4 个答案:

答案 0 :(得分:1)

只要您对textField有一个引用,它只是一个属性

textField.text将返回当前键入textField的内容。

答案 1 :(得分:0)

如果您在interface文件中声明了文本字段,则可以通过它的实例变量访问它:

NSLog(@"text from textfield: %@", dynamicTextField.text);

或通过财产:

NSLog(@"text from textfield: %@", self.dynamicTextField.text);

但是如果你在for循环中创建了许多文本字段,请执行以下操作:

创建textField时,请说:

dynamicTextField.tag = x;

其中“x”是循环的递增整数。

现在,要获取它的价值,请说:

for(UITextField *texter in self.view.subviews)
   if(texter.tag == 1)//or any other tag number you have specified on creating the textField
       NSLog(@"textfield with tag 1 contains text: %@", texter.text);

您只需记住为所需的文本字段分配了哪个标记号

答案 2 :(得分:0)

您已经创建了 TextField 对象的属性,然后您可以访问它txtFiled.text[txtFiled text]

答案 3 :(得分:0)

在此方法- (void)textFieldDidEndEditing:(UITextField *)textField中,参数是键入用户的文本字段。

使用textField.text从文本字段中获取数据,使用textField.tag获取标记(用于标识文本字段)。

NSlog(@"%@",textField.text); //print the text content
NSlog(@"%d",textField.tag); //print the tag