Pradeep Reddy Kypa
Pradeep Reddy Kypa

Reputation: 4022

How to get the count of a Particular Subview in a UIView?

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?

Upvotes: 1

Views: 2830

Answers (2)

Pradeep Reddy Kypa
Pradeep Reddy Kypa

Reputation: 4022

Got it!!!!

-(NSUInteger)getTotalNumberOfTextViewsAddedInTheView{

NSUInteger count = 0;

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

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

        count ++;
    }
}

return count;
}

Upvotes: 0

Muzahid
Muzahid

Reputation: 5186

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.

Upvotes: 1

Related Questions