从nsmutablearray中删除对象

时间:2013-04-11 06:41:43

标签: iphone ios xcode nsmutablearray uitextfield

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

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

}

我只想逐个删除文本字段,请提供任何帮助

2 个答案:

答案 0 :(得分:5)

您正在从阵列中删除对象。您还需要使用removeFromSuperview

从superView中删除它
- (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;    
}

答案 1 :(得分:3)

UITextField *txtField = [txtFieldArray lastObject];
[txtField removeFromSuperView];