2013-04-11 151 views
0

我正在通過按鈕操作在數組中逐個添加textfields,同樣我想從另一個按鈕中刪除這些文本字段,我應該怎麼做我正在使用此代碼添加從nsmutable陣列中刪除對象

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

} 

我只是想刪除文本框一個接一個,任何幫助,請

+0

因此,您正在通過'removeLastObject'刪除,那麼問題是什麼呢? – 2013-04-11 06:43:27

+0

它不是刪除 – NullData 2013-04-11 06:43:44

+0

我想刪除最後一個對象,如果按下按鈕,然後再次按下按鈕,直到數組計數爲零或用戶停止按下按鈕 – NullData 2013-04-11 06:44:46

回答

5

您正在從陣列中刪除對象。您還需要將其從超級視圖中刪除它使用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;  
} 
+0

偉大的工作 – NullData 2013-04-11 06:48:48

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