2017-06-01 24 views
-2

如何從Scroll子視圖中移除UITextField的數組,最初在我調用API時我根據Count創建了一些數組值我已經在My View中創建了Dynamic UITextFiled但我知道如何刪除子視圖如何從子視圖中刪除動態UITextField

這裏我的示例代碼:

// NSArray *strings; 
// UIScrollView *scrollView; 
// NSMutableArray *textFields; 

self.textFields = [NSMutableArray array]; 

const CGFloat width = 320; 
const CGFloat height = 31; 
const CGFloat margin = 0; 
CGFloat y = 0; 

for(NSString *string in strings) { 
UITextField *textField = [[UITextField alloc] 
initWithFrame:CGRectMake(0, y, width, height)]; 
textField.delegate = self; 
textField.text = string; 

[scrollView addSubview:textField]; 
[textFields addObject:textField]; 
[textField release]; 

y += height + margin; 
} 

scrollView.contentSize = CGSizeMake(width, y - margin); 
+0

你想刪除所有的textFields? –

+0

爲每個文本字段使用標籤(簡單地使用數組索引作爲標籤)。所以你可以刪除你需要刪除的任何文本字段。 – jegadeesh

+0

你想只刪除那些使用這種方法添加的textField? –

回答

1

杉杉任何文本視圖。

for(NSString *string in strings) { 
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, y, width, height)]; 
    textField.delegate = self; 
    textField.text = string; 
    //Set tag 101 
    textField.tag = 101; 
    [scrollView addSubview:textField]; 
    [textFields addObject:textField]; 
    [textField release]; 

    y += height + margin; 
} 

現在創建一個方法來刪除所有這些動態textFields。

- (void)removeAllDynamicTextFields { 

    for(UIView *view in scrollView.subViews){ 
     if([view isKindOfClass:[UITextField class]] && view.tag == 101){ 
       [view removeFromSuperview]; 
      } 
     } 
    } 
} 
+0

夢幻般的兄弟工作就像一個國王 – batMan007

+0

@ batMan007歡迎隊友:) –

0

如果你想從您的視圖刪除文本框,然後你可以簡單地removi從上海華盈是文本框。當你創建這個動態textField創下單tag101

[yourtextfield removeFromSuperView]; 
0
scrollView.subViews.forEach({textField in 
      if textField is UITextField , textField.tag == XX { 
       textField.removeFromSuperView() 
      } 
     }) 
+1

這是Swift不是目標C –

+0

你是對的Dude ... –

-1

添加標籤,然後刪除與循環所有的

for (UITextView *i in scrollView){ 
    if([i isKindOfClass:[UITextView class]]){ 
     UITextView *tv = (UITextView *)i; 
     if(tv.tag == 1){ // Whatever textview want to remove add tag here 
      /// Write your code 
      [tv removeFromSuperview]; 
     } 
    } 
}