Reputation: 873
Here is what I'd like to do.
I have 5 labels . I would like to change attributes (background color), using a variable. So if the variable (x) is equal to 3, it would change the background on label03.
At the same time, there will be 5 images . I would change the attributes (alpha), using the same variable. So if the variable (x) is equal to 3 like above, it would change the label03 background, and the alpha on image03.
What I'm missing is the way to choose the proper tag so (x) is pointing to what I need it to point to.
Upvotes: 1
Views: 735
Reputation: 62686
I think you should do something like this:
// create an offset by control type (zero is not a good value here)
#define kLABEL_TAG_OFFSET 32
#define kIMAGE_TAG_OFFSET 64
// when creating the controls
UILabel *label0 = [[UILabel alloc] initWithFrame:...];
label0.tag = kLABEL_TAG_OFFSET + 0;
// and so on 0..3
label4.tag = kLABEL_TAG_OFFSET + 4;
imageView4.tag = kIMAGE_TAG_OFFSET + 4
// then, when you want to alter a particular image and label
//
- (void)setPropertiesOnControlsIndexedBy:(NSInteger)index {
UILabel *label = (UILabel *)[self.view viewWithTag:kLABEL_TAG_OFFSET + index];
UImageView *image = (UImageView *)[self.view viewWithTag:kIMAGE_TAG_OFFSET + index];
label.backgroundColor = [UIColor greenColor];
image.alpha = 0.5;
}
Upvotes: 1
Reputation: 13266
Assuming that you're using a nib to create and position these elements, all you'll need to do is open the Attributes Inspector and set the tag for each of the labels and images. I would go with giving the labels a tag of 1x and the images a tag of 2x (where x is the number of the element). So the labels have tags 10-14 and the images have tags 20-24. Then in your viewController, if you have a int x, you can get the label and imageViews with the following lines.
UILabel *label = [self.view viewWithTag:10+x];
UIImageView *imageView = [self.view viewWithTag:20+x];
Upvotes: 1
Reputation: 24481
Call -reloadData
when you want to change x, and have the default values set to x, so upon reload, they will automatically change.
- (void) xChanged {
[self.tableView reloadData];
}
- (UITableViewCell *) tableView:(UITableView *) tv cellForRowAtIndexPath:(NSIndexPath *) ip {
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image-%d", x]];
cell.imageView.alpha = x;
}
Upvotes: 0
Reputation: 4357
Create/customize all the labels and images in the viewDidLoad
and everytime you change the value of the variable, tell the view to reload.
[someView reloadData];
Upvotes: 0