简体   繁体   English

在子视图中查找所有UIButton

[英]Finding all UIButtons in subviews

I have a single UIButton in the view of my UIViewController . 我的UIViewController视图中有一个UIButton I also have ten more that are in a subview in the main view. 主视图的subview中还有十个subview视图。 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? 不应该迭代每个subview然后找到它们吗?

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: 像这样使用Objective-C块的递归函数将在给定视图的视图层次结构中的测试块中找到给定子类类型的所有视图:

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) . 如果您的所有按钮都已经在self.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];


                        }

                    }

                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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