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

以下代码我用...

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


在上面的代码中,我正确地获得了标签,但是当我在其他Mathod上使用标签时,我仅获得了最后一个textfields标签。

谢谢,
拉胡尔·维尔贾(Rahul Virja)

最佳答案

您覆盖txtQuestions变量。


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


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

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

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

[_textFieldArray objectAtIndex:txtQuestions.tag]


清楚吗?

07-26 03:57