Skip to content

Instantly share code, notes, and snippets.

@rdeguzman
Created March 7, 2011 07:47
Show Gist options
  • Save rdeguzman/858199 to your computer and use it in GitHub Desktop.
Save rdeguzman/858199 to your computer and use it in GitHub Desktop.
how to pass arraySection element or [arraySection objectForKey:@"id"] to a button?
#define PADDING_VERTICAL 5.0f
#define BUTTON_HEIGHT 40.0f
#define BUTTON_WIDTH 160.0f
#define BUTTON_ORIGIN_X (320.0f - BUTTON_WIDTH)/2.0f
#define BUTTON_ORIGIN_Y 20.0f
- (void)initButtons{
NSLog(@"MainViewController.initButtons");
if(arraySections == nil){
DataAccess *da = [[DataAccess alloc] init];
arraySections = [[[NSMutableArray alloc] initWithArray:[da getMainSections]] autorelease];
[da release];
}
CGFloat totalHeight = BUTTON_ORIGIN_Y;
UIImage* buttonImage = [UIImage imageNamed:@"button_center_light_gray.png"];
for(NSDictionary* section in arraySections){
NSString* title = [section objectForKey:@"title"];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(BUTTON_ORIGIN_X, totalHeight, BUTTON_WIDTH, BUTTON_HEIGHT);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];
[self.view addSubview:button];
totalHeight = totalHeight + BUTTON_HEIGHT + PADDING_VERTICAL;
}
}
- (void)buttonPressed:(id)sender{
UIButton* button = (UIButton*)sender;
NSLog(@"MainViewController.buttonPressed %@ tag: %@", [button currentTitle], [button tag]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment