0

我必须在 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;
    }

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

谢谢, 拉胡尔·维尔加

4

1 回答 1

1

你覆盖txtQuestions变量。

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

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

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

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

[_textFieldArray objectAtIndex:txtQuestions.tag]

清楚吗?

于 2012-09-06T12:22:17.267 回答