如何从Subview中删除Dynamic UITextField

时间:2017-06-01 12:31:42

标签: ios iphone subview

如何从Scroll Subview中删除UITextField数组,最初在我调用API时我得到一些基于Count的数组值我在My View中创建了动态UITextFiled,但我知道如何删除子视图

这里是我的示例代码:

// 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);

4 个答案:

答案 0 :(得分:1)

当您创建动态textFieldtag 101时,首先要做的事情。

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];
            }
        }
    }
}

答案 1 :(得分:0)

如果您想从视图中删除该文本字段,那么您只需从超级视图中删除该文本字段。

[yourtextfield removeFromSuperView];

答案 2 :(得分:0)

scrollView.subViews.forEach({textField in
            if textField is UITextField , textField.tag == XX {
                textField.removeFromSuperView()
            }
        })

答案 3 :(得分:-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];
        }
    }
}