简体   繁体   中英

Finding all UIButtons in subviews

I have a single UIButton in the view of my UIViewController . I also have ten more that are in a subview in the main view. I want to find all these buttons. So far I have:

-(void)findAllButtons{

    for(UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[myButton class]]){
            NSLog(@"found a button!");         
        }
    }
}

It is only finding the single button though and not the other ten. Why is that? Shouldn't it iterate every single subview and then find them?

for (UIView *subView in scroll.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton*)subView;
            if (btn.tag == selectedButton.tag) {
                btn.layer.borderWidth = 1.0f;
                btn.layer.borderColor = [UIColor darkGrayColor].CGColor;
            }else{
                btn.layer.borderWidth = 1.0f;
                btn.layer.borderColor = [UIColor clearColor].CGColor;
            }
        }
    }

A recursive function using Objective-C blocks like this will find all views of a given subclass type as specified in the test block in the view hierarchy of the given view:

NSMutableArray *marrAllButtons = [NSMutableArray new];

BOOL (^viewTest)(UIView*) = ^BOOL(UIView* viewToTest) {

    return [view isKindOfClass:[UIButton class]];
};

void(^viewEnumerator)(UIView*) = ^(UIView* outerView){

    for (UIView *view in outerView.subviews)
    {
        if (viewTest(view))
        {
            [marrAllButtons addObject:view];
        }
        else
        {
            viewEnumerator(view);
        }
    }
};

viewEnumerator(self.view);

NSLog(@"All Buttons %@", marrAllButtons);

Just a few lines of code

-(void)findAllButtons {
    [self findButtonsInSubviews:self.view.subviews];
}

- (void)findButtonsInSubviews:(NSArray *)subviews {
    for(UIView *view in subviews) {
        if ([view isKindOfClass:[UIButton class]]){
            NSLog(@"found a button!");
        } else {
            [self findButtonsInSubviews:view.subviews];
        }
    }
}
- (NSMutableArray *)buttonsInView:(UIView *)view
{
    NSArray *subviews = view.subviews;
    NSMutableArray *buttons = [NSMutableArray array];

    for (UIView *subview in subviews)
    {
        if ([subview isKindOfClass:[UILabel class]])
        {
            [buttons addObject:subview];
        }
        else if(subview.subviews)
        {
            [buttons addObjectsFromArray:[self buttonsInView:subview]];
        }
    }
    return buttons;
}

Your method is right if all your button already have in self.view (main view) .

Just set tag for all button to check and also make sure that all button are on the main view . I hope this will work.

-(void)findAllButtons{

for(UIView *view in self.view.subviews) {
    if ([view isKindOfClass:[myButton class]]){
       UIButton *button = (UIButton*)view;
        NSLog(@"found a button with tag:%d",button.tag);         
    }
  }
}
for(UIView * subView in view.subviews) // here write Name of you ScrollView.

 {

     // NSLog(@"test %@", [subView class]);

                    if([subView isKindOfClass:[UIButton class]])

                    {

                        UIButton *button = (UIButton*)subView;

                        [button setSelected:NO] ;


NSString *s1;

s1 = @",";

s1 = [s1 stringByAppendingString:[NSString stringWithFormat:@"%@",button.titleLabel.text ]];


s1 = [s1 stringByAppendingString:[NSString stringWithFormat:@"%@",@"," ]];


                   NSRange range = [temp_Colors_Name_comma rangeOfString:s1  ];

                        if(range.location == NSNotFound)

                        {



                        }

                        else

                        {

                            [button setSelected:YES];


                        }

                    }

                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM