Newbee
Newbee

Reputation: 265

Issue in deleting the textfield

I have two buttons for adding and deleting the textfields. Adding works fine but when I click on delete it deletes only the last added textfield. Here are my two methods:

-(void)addTextField {
    keyTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, yAxisDistance, 150, 30)];
    keyTextField.borderStyle = UITextBorderStyleRoundedRect;
    keyTextField.placeholder = @"Key Value";
    keyTextField.delegate = self;
    [self.view addSubview:keyTextField];

    valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(165, yAxisDistance, 150, 30)];
    valueTextField.borderStyle = UITextBorderStyleRoundedRect;
    valueTextField.placeholder = @"Value";
    valueTextField.delegate = self;
    [self.view addSubview:valueTextField];
    yAxisDistance = yAxisDistance+35;
}

-(void)deleteTextField {
    [keyTextField removeFromSuperview];
    [valueTextField removeFromSuperview];
    yAxisDistance = yAxisDistance-35;
}

I know it's an small issue but I am very new to this field so kindly help.

Upvotes: 0

Views: 132

Answers (4)

Bhumeshwer katre
Bhumeshwer katre

Reputation: 4671

Sorry that time I was not understand your problem...

I done with this: Declare one

NSMutableArray *allTextfieldArray;

and initialise in

viewdidload

method.. now do:

-(void)addTextField {
    keyTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, yAxisDistance, 150, 30)];
    keyTextField.borderStyle = UITextBorderStyleRoundedRect;
    keyTextField.placeholder = @"Key Value";
    keyTextField.delegate = self;
    [self.view addSubview:keyTextField];

    valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(165, yAxisDistance, 150, 30)];
    valueTextField.borderStyle = UITextBorderStyleRoundedRect;
    valueTextField.placeholder = @"Value";
    valueTextField.delegate = self;

    [self.view addSubview:valueTextField];

    yAxisDistance = yAxisDistance+35;

    [allTextfieldArray addObject:keyTextField];
    [allTextfieldArray addObject:valueTextField];

}


if ([allTextfieldArray count]>0) {
    UITextField *txtField =     [allTextfieldArray lastObject];
    [allTextfieldArray removeLastObject];

    [txtField removeFromSuperview];
    txtField = nil;


    UITextField *txtField2 =      [allTextfieldArray lastObject];
    [allTextfieldArray removeLastObject];

    [txtField2 removeFromSuperview];
    txtField2 = nil;

    yAxisDistance = yAxisDistance-35;

}

Upvotes: 1

Yunus Nedim Mehel
Yunus Nedim Mehel

Reputation: 12369

I think the problem lies with the outlet. Can you check the following:

  1. Open the outlets that are connected to the textifeld in the IB. There should be gray dots inside circles on the left side of the editor. Are they seem correct for both text fields?

  2. Setup a breakpoint inside deleteTextField method and check the two textfields. Verify that both properties are not nil.

PS: You don't need to add tags to your view, using properties is perfectly fine and even better in my opinion. The reason for your problem is something else. Also, you do not need to removeFromSuperview, you can also setHidden:YES.

Upvotes: 1

thorb65
thorb65

Reputation: 2716

you store the last textField in your variables keyTextField and valueTextField. So when you call your deleteTextField the last both will be deleted. you must track which textFileds you want exactly to delete.

for example you could give all your textFields a tag number when you create them: first create an int as counter:

@implementation YourClass {
    int tagcounter;
}

in init method set your counter:

tagcounter = 0;

in your addTextField:

keyTextField.tag = tagcounter++;
valueTextField.tag = tagcounter++;

when the delete button is tapped, you must know the textfield tags and pass them to your deleteTextField method. There you could do something like:

-(void)deleteTextFieldWithTag:(int)tagnumber {
for (UIView *view in [self.view subviews])
{
    if ([view isKindOfClass:[UITextField class]])
    {
        if (view.tag == tagnumber || view.tag == tagnumber+1) {
            [view removeFromSuperview];
    }
}
}

Upvotes: 0

Pradhyuman sinh
Pradhyuman sinh

Reputation: 3928

Use this code for remove specific textfield from UIView. But First you have to set tag of every UITextField in view when you create or add it in view.

for ( UITextField* textField in view.subviews ) 
{
    if(textField.tag== 1)
    {
          [textField removeFromSuperview];
    }
}

Upvotes: 1

Related Questions