在 iphone sdk 中动态创建多个 UITextfield

Posted

技术标签:

【中文标题】在 iphone sdk 中动态创建多个 UITextfield【英文标题】:Multiple UITextfield Create Dynamically in iphone sdk 【发布时间】:2012-09-06 11:57:08 【问题描述】:

我必须在 ipad 应用程序的滚动视图上动态创建多个文本字段。文本字段创建成功,我在单击的文本字段上使用弹出视图,但是当我单击文本字段时,我没有得到文本字段的标记。我得到了最后一个文本字段的标签,所以我无法在当前文本字段上设置文本。

以下我使用的代码...

int j = 430;
int k = 413;
int RB = 423;

for (int i=0; i<[appDelegate.questions count]; i++) 

    imgTxtQue = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"textbox.png"]];
    imgTxtQue.frame = CGRectMake(267, k, 490, 60);

    //  UIImageView *imgRatingBtn = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    //  imgRatingBtn.frame = CGRectMake(633, RB, 102, 39);
    //ratingButton1.hidden = NO;
    ratingButton1 = [[UIButton alloc] initWithFrame:CGRectMake(633, RB, 102, 39)];
    [ratingButton1 setImage:[UIImage imageNamed:@"rating_selected.png"] forState:UIControlStateNormal];

    txtQuestions = [[UITextField alloc] initWithFrame:CGRectMake(296, j, 429, 24)];
    //txtQuestions.tag = i;
    [txtQuestions setTag:i];
    NSLog(@"%d", txtQuestions.tag);
    txtQuestions.delegate = self;
    [txtQuestions setPlaceholder:[[appDelegate.questions objectAtIndex:i] objectForKey:@"question"]];
    txtQuestions.borderStyle = UITextBorderStyleNone;
    // txtQuestions.background = imgTxtQue.image;



    [scrView addSubview:imgTxtQue];
    [scrView addSubview:ratingButton1];
    [scrView addSubview:txtQuestions];


    j = j+50;
    k = k+50;
    RB = RB+50;




    -(void)loadQuestion


            txtQuestionOne.hidden=NO;

            imgTxtQue.hidden=NO;

            CGRect aFrame1 = imgTxtQue.frame;

            aFrame1.size.width = aFrame1.size.width + 200;
           // aFrame.size.height = newHeight;

            NSString *strQue = [NSString stringWithFormat:@"%@",[[appDelegate.questions objectAtIndex:0] objectForKey:@"question"]];


            CGSize newSize1 = [strQue sizeWithFont: [UIFont fontWithName: @"TrebuchetMS" size: 12] ];

            if (strQue.length > 42) 

            imgTxtQue.frame = CGRectMake(267, 413, newSize1.width+353, 60);
            [txtQuestionOne setFrame:CGRectMake(294, 430, newSize1.width+350, 24)];
                ratingButton1.frame = CGRectMake(855, 423, 102, 39);
                ratingLabel1.frame = CGRectMake(900, 430, 42, 21);

            

            [txtQuestionOne setPlaceholder:[[appDelegate.questions objectAtIndex:0] objectForKey:@"question"]];

            appDelegate.dynamicQues =[[appDelegate.questions objectAtIndex:0] objectForKey:@"question"];

            NSLog(@"current q1 %@", [[appDelegate.questions objectAtIndex:0] objectForKey:@"question"]);

            if ([[[appDelegate.questions objectAtIndex:0] objectForKey:@"permission"] isEqualToString:@"1"]) 

                ratingButton1.hidden=NO;

                ratingLabel1.hidden=NO;
            






- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
   
    BOOL editing=YES;

    for (UIImageView *img in ViewPost.subviews)
       
        if ([img isKindOfClass:[UIImageView class]]) 
        
            if (img.tag==textField.tag) 
            
                [img setImage:[UIImage imageNamed:@"Redtextbox.png"]];;
            
            else   
                if (img.tag!=0) 
                    if (img.tag != 10) 

                        [img setImage:[UIImage imageNamed:@"textbox.png"]];
                    
                    else
                    
                        [img setImage:[UIImage imageNamed:@"inputbox.png"]];
                    

                

            
                

    

    if (textField==txtcategory) 

        [textField resignFirstResponder];

        appDelegate.category=YES;

        editing=NO;

        touchflag=1;

        [self performSelectorOnMainThread:@selector(CategoryListClick) withObject:nil waitUntilDone:YES];
        //[self CategoryListClick];

        return NO;
    

    if (textField==txtsubcategory) 

        appDelegate.category=NO;

        editing=NO;

        [self performSelectorOnMainThread:@selector(CategoryListClick) withObject:nil waitUntilDone:YES];
    
    if (txtQuestions.tag) 

   // if (textField==txtQuestions) 
        editing=NO;
        index=txtQuestions.tag;
        NSLog(@"%d",txtQuestions.tag);
        touchflag = 1;
        [self performSelectorOnMainThread:@selector(loadAnswers) withObject:nil waitUntilDone:YES];
   // 
    


-(void)loadAnswers

    if (touchflag==1) 

        [Name resignFirstResponder];
        [Email resignFirstResponder];
        [txtCityCode resignFirstResponder];
        [txtcategory resignFirstResponder];
        [txtTitle resignFirstResponder];
        [txtQuestions resignFirstResponder];
        [txtsubcategory resignFirstResponder];

        SubcategoryPopViewController *objPopview = [[SubcategoryPopViewController alloc]initWithNibName:@"SubcategoryPopViewController" bundle:nil];

        objPopview.delegate = self;

        objPopview.index=txtQuestions.tag;

        popView =[[UIPopoverController alloc]initWithContentViewController:objPopview];
        [self RescheduleTimer];

                [popView presentPopoverFromRect:imgTxtQue.frame inView:ViewPost permittedArrowDirections:0 animated:NO];

                [popView setPopoverContentSize:CGSizeMake(376, 260)];

    


-(void) didSelectAnswer

    touchflag=0;

    [popView dismissPopoverAnimated:YES];
    [self RescheduleTimer];
    if (appDelegate.ansDynamic==YES) 

       // txtQuestionOne.text=appDelegate.answerOne;
        if (txtQuestions.tag) 

        txtQuestions.text = appDelegate.answerDynamic;
        

        appDelegate.ansDynamic=NO;

        questionIndex=txtQuestions.tag;

        if (![ratingButton1 isHidden]) 

            ratingButton1.enabled=YES;

            RatingPopViewController *objPopview = [[RatingPopViewController alloc]initWithNibName:@"RatingPopViewController" bundle:nil]; 

            objPopview.index=txtQuestions.tag;

            objPopview.checkedCell=ratingLabel1.text;

            objPopview.QuestionText=appDelegate.answerDynamic;

            objPopview.delegate=self;

            popSweepStakes =[[UIPopoverController alloc]initWithContentViewController:objPopview];

            [popSweepStakes presentPopoverFromRect:ratingButton1.frame inView:ViewPost permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];


            [popSweepStakes setPopoverContentSize:CGSizeMake(300, 400)];

        

        return;
    

在上面的代码中,我正确地得到了标签,但是当我在其他数学上使用标签时,我只得到最后一个文本字段标签。

谢谢, 拉胡尔·维尔贾

【问题讨论】:

您能否提供您在尝试获取标签时使用的代码? if (txtQuestions.tag) // if (textField==txtQuestions) editing=NO;索引=txtQuestions.tag; NSLog(@"%d",txtQuestions.tag);触摸标志 = 1; [self performSelectorOnMainThread:@selector(loadAnswers) withObject:nil waitUntilDone:YES]; // 如果你编辑你的问题会更易读:) 我已经加载了三个问题文本字段,但我只得到了最后一个文本字段标签。 那是因为在您的“if(txtQuestions.tag)”中,txtQuestions 是您创建的最后一个元素!您创建文本字段 1、2、3,因此 txtQuestions 指向最后一个。您尝试编辑 txtQuestions,但您没有更改它所指向的内容。我会尝试写一个解决方案 【参考方案1】:

您覆盖了txtQuestions 变量。

    NSMutableArray * _textFieldArray 这样声明一个类属性。 (这是导入声明一个NSMutableArray 对象以添加您的文本字段)。 在您的类的 init 方法中分配并初始化您的数组:_textFieldArray = [[NSmutableArray alloc]init]。 在您的for 语句中,使用以下代码分配一个文本字段:

UItextField * txtQuestions = [[UITextField alloc] initWithFrame:CGRectMake(296, j, 429, 24)];

txtQuestions 对象上设置所有属性后,将其添加到数组中 [_textFieldArray addObject:txtQuestions]

当您需要文本字段时,请使用以下代码:

[_textFieldArray objectAtIndex:txtQuestions.tag]

清楚了吗?

【讨论】:

我使用你的建议我不能在方法之外使用文本字段 您可以将您的 textField 添加到一个数组中并使用 tag 属性检索它。清楚吗?您需要示例代码吗?

以上是关于在 iphone sdk 中动态创建多个 UITextfield的主要内容,如果未能解决你的问题,请参考以下文章

如何管理单行中的多个标签但没有。表格视图中的单元格数量取决于 iPhone SDK 中的数组。

如何在 Iphone SDK 中基于单个数组对多个数组进行排序

分组UITableView,多个部分在iPhone SDK中创建问题

Facebook iOs sdk iphone:从多个视图控制器调用

如何从 iphone sdk 将多个图像上传到 facebook

iPhone SDK:禁用在文本字段(或文本视图)中自动创建点 (.)