1

I want the count of a particular subview inside a UIView. I am adding some multiple textviews to my view and I want the count of the total UITextviews added to the view? I am able to iterate inside the subviews of the array but not able to find the exact count of the UITextViews. Any Idea?

Pradeep Reddy Kypa
  • 3,992
  • 7
  • 57
  • 75

2 Answers2

1

Swift 2:

func numberOfTexViewInView(superView: UIView)-> Int{

        var count = 0

        for _view in superView.subviews{

            if (_view is UITextView){
                count++
            }

        }

        return count
    }

Objective C:

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

    NSUInteger count = 0;

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

    return count;

}

Pass your main view in which your UITexView added as parameter to the function and get the result.

Muzahid
  • 5,072
  • 2
  • 24
  • 42
0

Got it!!!!

-(NSUInteger)getTotalNumberOfTextViewsAddedInTheView{

NSUInteger count = 0;

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

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

        count ++;
    }
}

return count;
}
Pradeep Reddy Kypa
  • 3,992
  • 7
  • 57
  • 75