0

我正在通过按钮操作在数组中一个一个地添加一个数组中的文本字段,同样我想从最后一个一个一个地删除另一个按钮中的这些文本字段,我应该怎么做我正在使用此代码添加

- (IBAction)btnAddTxtField:(id)sender {
   // [self buttonPressed];
//    currentIndex =0;
//    
//    int x = 132;
//    int y = 550;
//    

//   
//    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake((currentIndex % 1) * x + 45, (currentIndex / 1) * y + 28, 214, 25)];
//    
//    
    textField = [[UITextField alloc] initWithFrame:CGRectMake(76, ([txtFieldArray count] * 30), 191, 25)];
    [textField setBorderStyle:UITextBorderStyleLine];
    textField.font = [UIFont systemFontOfSize:20];
    textField.placeholder = @"Enter text";
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.returnKeyType = UIReturnKeyDone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textField.delegate = self;
    [myScrollview addSubview:textField];
    [txtFieldArray addObject:textField];


    CGRect frame = bottomView.frame;
    frame.origin.y += textField.frame.size.height + 5;

    bottomView.frame = frame;
    textField.hidden = NO;

}

并用于删除

- (IBAction)btnRemovTxtField:(id)sender {

   [txtFieldArray removeLastObject];
    textField.hidden = YES;
    CGRect frame = bottomView.frame;
    frame.origin.y -= textField.frame.size.height - 5;
    bottomView.frame = frame;

}

我只想一个一个地删除文本字段,请帮忙

4

2 回答 2

5

您正在从数组中删除对象。您还需要使用从 superView 中删除它removeFromSuperview

- (IBAction)btnRemovTxtField:(id)sender {
    UITextField *txtField = [txtFieldArray lastObject];
    [txtField removeFromSuperview];

    [txtFieldArray removeLastObject];
    textField.hidden = YES;
    CGRect frame = bottomView.frame;
    frame.origin.y -= textField.frame.size.height - 5;
    bottomView.frame = frame;    
}
于 2013-04-11T06:46:26.040 回答
3
UITextField *txtField = [txtFieldArray lastObject];
[txtField removeFromSuperView];
于 2013-04-11T06:45:49.473 回答