title: How to handle dynamically created UIButtons
body: <p>I have a UIViewController that acts as a sort of menu.  The menu is made up of multiple boxes/tiles which are UIViews that contain a UILabel with a description, a UIButton that links to another UIViewController that shows more information and another UIButton that links to the UIViewController that show another area of the app.</p>
  
  <p>At the moment there are 3 of these boxes but this will grow.  At the moment I have hardcoded each one like this:</p>
  
  <pre><code>- (void)drawProceduresBox
  {
  self.viewProceduresBox = [[UIView alloc] initWithFrame:CGRectMake((_screenWidth * 0.05), (_screenHeight * 0.35), (_screenWidth * 0.9), (_screenHeight * 0.25))];
  [_viewProceduresBox setBackgroundColor:[UIColor whiteColor]];
  _viewProceduresBox.layer.borderWidth = 1.0f;
  _viewProceduresBox.layer.borderColor = [UIColor grayColor].CGColor;
  
  CGFloat boxHeight = _viewProceduresBox.frame.size.height;
  CGFloat boxWidth = _viewProceduresBox.frame.size.width;
  
  self.buttonShowProcedures = [[UIButton alloc] init];
  [_buttonShowProcedures setFrame:CGRectMake((boxWidth * 0.8), (boxHeight * 0.4), (boxWidth * 0.1), (boxHeight * 0.2))];
  [_buttonShowProcedures setBackgroundColor:[UIColor greenColor]];
  [_buttonShowProcedures addTarget:self action:@selector(showProcedures) forControlEvents:UIControlEventTouchUpInside];
  
  _buttonshowMoreInfo = [[appButton alloc] init];
  [_buttonshowMoreInfo setFrame:CGRectMake((boxWidth * 0.6), (boxHeight * 0.6), (boxWidth * 0.1), (boxHeight * 0.2))];
  [_buttonshowMoreInfo setBackgroundColor:[UIColor blueColor]];
  [_buttonshowMoreInfo setWhatToShow:@"vetMed"];
  [_buttonshowMoreInfo addTarget:self action:@selector(showMoreInfo:) forControlEvents:UIControlEventTouchUpInside];
  
  [_viewProceduresBox addSubview:_buttonShowProcedures];
  </code></pre>
  
  <p>} </p>
  
  <p>There could be 10+ of these boxes so I wanted a way to create them dynamically.  This is what I have tried so far:</p>
  
  <pre><code>-(void)drawBox:(NSString*)moduleName boxFrame:(CGRect)boxFrame labelDesc:(NSString*)labelDesc
  {
  
  UIView *moduleInfoBox = [[UIView alloc] initWithFrame:boxFrame];
  [moduleInfoBox setBackgroundColor:[UIColor purpleColor]];
  
  CGFloat boxHeight = moduleInfoBox.frame.size.height;
  CGFloat boxWidth = moduleInfoBox.frame.size.width;
  
  UILabel *moduleLable = [[UILabel alloc] init];
  [moduleLable setFrame:CGRectMake((boxWidth * 0.05), (boxHeight * 0.05), (boxWidth * 0.6), (boxHeight * 0.9))];
  [moduleLable setBackgroundColor:[UIColor yellowColor]];
  [moduleLable setText:labelDesc];
  
  UIButton *showModuleButton = [[UIButton alloc] init];
  [showModuleButton setFrame:CGRectMake((boxWidth * 0.8), (boxHeight * 0.15), (boxWidth * 0.1), (boxWidth * 0.2))];
  [showModuleButton setBackgroundColor:[UIColor grayColor]];
  [showModuleButton addTarget:self action:@selector(showModule:) forControlEvents:UIControlEventTouchUpInside];
  
  UIButton *showMoreInfoButton = [[UIButton alloc] init];
  [showMoreInfoButton setFrame:CGRectMake((boxWidth * 0.8), (boxHeight * 0.65), (boxWidth * 0.1), (boxWidth * 0.1))];
  [showMoreInfoButton setBackgroundColor:[UIColor orangeColor]];
  [showMoreInfoButton addTarget:self action:@selector(showMoreInfo:) forControlEvents:UIControlEventTouchUpInside];
  
  [moduleInfoBox addSubview:moduleLable];
  [moduleInfoBox addSubview:showModuleButton];
  [moduleInfoBox addSubview:showMoreInfoButton];
  
  _scrollHeight = _scrollHeight + (moduleInfoBox.frame.origin.y + moduleInfoBox.frame.size.height);
  
  [_scrollView addSubview:moduleInfoBox];
  
  
  }
  </code></pre>
  
  <p>And I would call the method like this (for three boxes in this case):</p>
  
  <pre><code>CGRect formularyFrame = CGRectMake((_screenWidth * 0.05), (_screenHeight * 0.05), (_screenWidth * 0.9), (_screenHeight * 0.25));
  CGRect proceduresFrame = CGRectMake((_screenWidth * 0.05), (_screenHeight * 0.35), (_screenWidth * 0.9), (_screenHeight * 0.25));
  CGRect vetMedFrame = CGRectMake((_screenWidth * 0.05), (_screenHeight * 0.65), (_screenWidth * 0.9), (_screenHeight * 0.25));
  [self drawBox:@"Formulary" boxFrame:formularyFrame labelDesc:@"Desc about formulary module"];
  [self drawBox:@"Procedures" boxFrame:proceduresFrame labelDesc:@"Desc about procedures module"];
  [self drawBox:@"VetMed" boxFrame:vetMedFrame labelDesc:@"Desc about vetMed module"];
  </code></pre>
  
  <p>The problem I am having is that the dynamically generated UIButtons need to link to relevant things, UIViewControllers in this case.  The buttons each have a selector that fires the methods 'showModule' and 'showModuleInfo'.</p>
  
  <p>In these methods, how do I tell what UIButton was pressed, and therefor, know which UIViewController to push the navigationController to?  Basically I would like something along the lines of:</p>
  
  <pre><code>-(void)showModule:(NSString*)moduleToShow
  {
      [self.navigationController pushViewController:moduleToShow animated:NO];
  }
  </code></pre>
  
  <p>But as I understand it, you cannot add parameters to '@selector' assignment:</p>
  
  <pre><code>[_buttonshowMoreInfo addTarget:self action:@selector(showMoreInfo:) forControlEvents:UIControlEventTouchUpInside];
  </code></pre>
  
  <p>What should I do instead?</p>