Meghan
Meghan

Reputation: 1014

Having Problem with dynamically created UITextField in iphone sdk

In my iphone app, I have created some UITextField in scrollView dynamically. And added one button in xib. On that button's TouchUpInside event, I opened UIImagePickercontroller to open photo library and taking selected image on UIImageView.

Now when modalview is dissmissed, values in my UITextField disappears.

How do I retain the UITextField values?

txt = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 30.0f, 200.0f, 30.0f)];

[txt addTarget:self action:@selector(keyDown:)forControlEvents:UIControlEventEditingDidEndOnExit];

txt.textColor = [UIColor blackColor];

txt.borderStyle = UITextBorderStyleBezel;

txt.autocorrectionType = UITextAutocorrectionTypeNo;

[fieldArray addObject:txt];

Using for loop I am adding txt to NSMutableArray fieldArray.

Here is the code where I fetch values from TextFields

NSMutableArray *insertValues = [[NSMutableArray alloc]init];

//Taking values from textFields

for (int i=1; i<=nooffields; i++) {

    UITextField *tf = (UITextField *)[self.view viewWithTag:i];

    NSLog(@"TF : %@",tf.text);

    if (tf.text.length<=0) {

        tf.text=@"";

    }

    [insertValues addObject:[NSString stringWithFormat:@"'%@'",tf.text]];
}

EDIT : And also when I display values from database in this textFields and try to edit it. It gives me null values.

What may be the reason? If there any other alternatives? please help me

Upvotes: 1

Views: 1017

Answers (3)

Mr.savaliya
Mr.savaliya

Reputation: 259

UITextField *textField=[[UITextField alloc]init];
[textField setFrame:CGRectMake(x,y, width, hight)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:textField];

Upvotes: 3

Meghan
Meghan

Reputation: 1014

I finally got a work around for my problem.

I am temporarily storing my textField's values into database when presentModalViewController is called.

These values I retrieve back into the textFields on viewWillAppear.

This may not be a very efficient solution but it worked in my case.

Hope this helps someone.

Thanks for all your responses.

Upvotes: 0

Fran Sevillano
Fran Sevillano

Reputation: 8163

The information that your scrollview uses to get populated should be stored outside the scrollview, in a NSMutableDictionary for example. So everytime cellForRowAtIndexPath gets called it can retrieve the information from that source. So I advise you to store the UITextField's text within the dictionary. You can index it by its tag number. That way you won't loose whatever it contained.

I hope it helps you

Cheers

Upvotes: 0

Related Questions