Skip to content

Instantly share code, notes, and snippets.

@elpsk
Created October 22, 2013 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elpsk/7100640 to your computer and use it in GitHub Desktop.
Save elpsk/7100640 to your computer and use it in GitHub Desktop.
foreach label
@interface APViewController ()
{
NSMutableArray *_Labels;
}
- ( void ) forEachLabel: ( void (^)( UILabel * label ) ) block;
@end
@implementation APViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_Labels = [[NSMutableArray alloc] init];
for ( int i=1; i<=15; i++ )
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake( i*20, 200, 20, 20 )];
label.text = @"x";
label.textColor = [ UIColor whiteColor ];
[ self.view addSubview:label ];
[ _Labels addObject:label ];
}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[ self forEachLabel:^(UILabel *label) {
label.textColor = [ UIColor yellowColor ];
}];
}
- ( void ) forEachLabel: ( void (^)( UILabel * ) ) block
{
for( UILabel * label in _Labels )
block( label );
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment