gpt4 book ai didi

iphone - 在iphone sdk中动态创建多个UITextfield

转载 作者:行者123 更新时间:2023-11-29 04:26:18 24 4
gpt4 key购买 nike

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

我使用以下代码...

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

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

谢谢,拉胡尔·维尔贾

最佳答案

您覆盖了 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]

清楚了吗?

关于iphone - 在iphone sdk中动态创建多个UITextfield,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12299578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com