Lozo Lozo - 5 months ago 17
Objective-C Question

How to loop through UIButtons with only certain border colors?

I am trying to loop through certain

UIButton
s with only certain border colors. For example, I am looking to loop through
UIButton
s that are only currently showing the border color light gray. I understand how to loop through all buttons in a view like this:

for (_buttonField in self.view.subviews)


But I cannot figure out how to only select
UIButton
s that are only currently bordered with the light gray color(or any specific color for that matter). I then want to delete these UIButtons with this specified UIColor border. I know this is done with removefromsuperview, but I am not sure what to call prior to that. I am actually trying to do this when I press a 'Delete' button in a UIAlertController.

Answer

You have to compare the colors using isEqual since you do not want to compare pointers but the color values.

for (UIButton *button in self.view.subviews) {
    if ([[UIColor colorWithCGColor:button.layer.borderColor] isEqual:[UIColor lightGrayColor]]) {
        [button removeFromSuperview];
    }
}