Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if subview is button or not

I am developing an application. In that I get all subviews of UITableViewCell.

The code for this one is:

    (void)listSubviewsOfView:(UIView *)view {

    // Get the subviews of the view
         NSArray *subviews = [view subviews];

    // Return if there are no subviews
    if ([subviews count] == 0) return;

    for (UIView *subview in subviews) {

        NSLog(@"%@", subview);

        // List the subviews of subview
        [self listSubviewsOfView:subview];
    }
}

But my problem is how to find out button from that subviews list. Please tell me how to solve this one.

like image 781
Venkat1282 Avatar asked Apr 19 '12 05:04

Venkat1282


2 Answers

You can iterate through all subviews like this.

for (id subview in subviews) {
   if ([subview isKindOfClass:[UIButton class]]) {
      //do your code
   }
}
like image 122
Martin Pilch Avatar answered Nov 14 '22 10:11

Martin Pilch


Swift

for subview in view.subviews {
    if subview is UIButton {
        // this is a button
    }
}
like image 26
Sahil Kapoor Avatar answered Nov 14 '22 11:11

Sahil Kapoor