Access each button in UIView on iPhone

What I want to do is ideal for all buttons in the UIView (I have many, more than 40 buttons), and depending on the tag, change the image displayed by the button. Does anyone know how I can do this?

Thanks!!!!

+3
source share
3 answers

Use the subview UIView property containing the buttons. For each UIView element in this NSArray, check the tag property. If the tag matches the needs of your logic, change the image displayed in this instance of the UIView, which in this case is one of the buttons.

+1
source

, subviews, devilaether, , , subview UIButton, - :

for(UIView *view in [rootView subviews]) {
    if([view isKindOfClass:[UIButton class]]) {
        if([view tag] == 0) {
            // First image
        } /* ... */
        else {
            NSLog(@"didn't recognize tag");
        }
     } else {
        NSLog(@"view is not a button");
     }
}

, - NSArray UIButtons, ; isKindOfClass:. .

+11
for(i=0; i<numberOfTags; i++){ 
UIButton *tempBtn = [yourView viewWithTag:i];
[tempBtn setImage:yourImage];

}

0
source

Source: https://habr.com/ru/post/1709723/


All Articles