0

I've 16 buttons in a view named as:

@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb1;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb2;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb3;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb4;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb5;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb6;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb7;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb8;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb9;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb10;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb11;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb12;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb13;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb14;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb15;
@property (strong, nonatomic) IBOutlet WKInterfaceButton *bb16;

I want to set the background of image based on different conditions in a switch statement. So what I'm doing is that I've a for loop

 for (int i=1; i<=16; i++) {
int val = [(NSNumber *)[self.valuesArray objectAtIndex:i] intValue];
    NSString *newString = [NSString stringWithFormat:@"bb%d",i];
    switch (val) {
                case 4:
                    [**self.bb%d** setBackgroundImageNamed:@"s"];
                    break;

                default:
                    break;
            }
    }

The val is the value it is currently picking from an NSArray and below that is a newString which is creating the required strings. The text inside ** is something I'm stuck at of-course I put the ** my self to highlight the problem. I want to change background image of the buttons based on different cases. So if it's case 4 than change it to s and if it's 0 change it to something else. So how can I achieve it. I hope my question is clear.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116
  • Why don't you just have the reference of parent view and assign each button a tag and fetch them when need from the referenced view by tag number? – Hamdullah shah Mar 22 '16 at 10:19
  • @Hamdullahshah Using tag for view identification is an antipattern. – Sulthan Mar 22 '16 at 11:17
  • 1
    @Hamdullahshah While that would work for iOS, a `WKInterfaceObject` wouldn't have a tag property. –  Mar 22 '16 at 17:17

1 Answers1

0

You can do this if you don't want much to be changed in your App's structure.

  1. Try putting all your 16 WKInterfaceButton objects in NSMutableArray in your initWithCoder method.(Make this array global if you want).

    #import "YourClass.h"
    
    @interface YourClass()
    
    @property (nonatomic,strong) NSMutableArray * btnArray;
    
    @end
    
    @implementation YourClass
    
    
    - (instancetype)initWithCoder:(NSCoder *)aDecoder
    {
         if (self = [super initWithCoder:aDecoder]) {
    
           _btnArray = [NSMutableArray array];
           [_btnArray addObject:bb1];
           /*
           .
           .
           .
           Add all 16 buttons like this here */
       }
       return self;
    } 
    
  2. At a particular case in your switch statement get object by index and then set it's background image.

    for (int i=1; i<=16; i++) {
    int val = [(NSNumber *)[self.valuesArray objectAtIndex:i] intValue];
    NSString *newString = [NSString stringWithFormat:@"bb%d",i];
     switch (val) {
            case 4:
                [((WKInterfaceButton *)[_btnArray objectAtIndex:i-1]) setBackgroundImageNamed:@"s"];
                  break;
    
              default:
                  break;
           }
       }
    
n.by.n
  • 2,458
  • 22
  • 31