如何在UIView中获取特定子视图的计数?

时间:2016-03-20 12:29:46

标签: ios uitextview subviews

我想在UIView中计算一个特定的子视图。我在视图中添加了一些多个textview,我想要在视图中添加总UITextviews的数量?我能够在数组的子视图内迭代,但无法找到UITextViews的确切数量。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

斯威夫特2:

func numberOfTexViewInView(superView: UIView)-> Int{

        var count = 0

        for _view in superView.subviews{

            if (_view is UITextView){
                count++
            }

        }

        return count
    }

目标C:

-(NSUInteger)numberOfTexViewInView:(UIView *)superView{

    NSUInteger count = 0;

    for (UIView *view in superView.subviews) {
        if ([view isKindOfClass:[UITextView class]]) {
            count ++;
        }
    }

    return count;

}

将您的UITexView添加的主视图作为参数传递给函数并获取结果。

答案 1 :(得分:0)

得到了!!!!

-(NSUInteger)getTotalNumberOfTextViewsAddedInTheView{

NSUInteger count = 0;

for (UIView *view in [self.view subviews]) {

    if ([view isKindOfClass:[UITextView class]]) {

        count ++;
    }
}

return count;
}