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?
Asked
Active
Viewed 2,770 times
2 Answers
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
-
but note that in your example the textviews have to be a **direct** subview of `self.view` to be taken into account! – André Slotta Mar 20 '16 at 12:45