Andira
Andira

Reputation: 13

Update Multiple Labels in objective c

@interface ViewController () @property (nonatomic,strong) NSMutableArray * gameB; @property (nonatomic,strong) NSMutableArray * rows; - (IBAction)initialize:(id)sender; - (BOOL) isEmpty; @end

- (IBAction)startGme:(id)sender {
    _rows = [[NSMutableArray alloc] initWithCapacity:4];
    _gameB = [[NSMutableArray alloc]initWithObjects :_rows , _rows ,_rows ,nil];
}

`

This is how my storyboard would look. These all values update from a 2-D array.I am new to objective c. What I am trying to do is update a few labels every time a change in my array happens. I know how to update one label. But if the number of labels is more than one is there a generic way to do that so everytime values in my array changes, my labels get the update or I need to update each label hard code? Any reference code would be helpful.

Upvotes: 1

Views: 146

Answers (2)

SwiftGod
SwiftGod

Reputation: 386

You can declare an outlet collection that is gonna look like that in swift:

    @IBOutlet var labelCollection: [UILabel]!

Then you have to link labels you want to update simultaneously to this declared collection in storyboard. Iterate over this collection and update all of it's contents like you want.

Upvotes: 1

GeneCode
GeneCode

Reputation: 7588

Just loop through the array and set the label.

[yourArray enumerateObjectsUsingBlock:^(id x, NSUInteger index, BOOL *stop){
   UILabel *label = (UILabel*)[self.view viewWithTag:index];
   NSString *str = (NSString*)x;
   [label setText:x];
}];

This assumes your labels has tag set 0-(num of labels), and that your array contains NSString.

Upvotes: 1

Related Questions