title: Passing selector to a custom method to create UIButton programmatically
body: <p>I am using XCode 5 under ios 7 to do a simple project. And I get the following error when I press a button that is created programmatically:</p>
  
  <pre><code>2013-11-10 09:16:02.969 Project2[446:70b] +[KNSunDynamicUIButton buttonSavePressed:]: unrecognized selector sent to class 0x221424
  </code></pre>
  
  <p>Details:<br>
  I create a custom method that is used to create a UIButton object programmatically.  That custom method is used in any view controller. That custom method needs passed-in parameters like x, y, width, height, button title, and selector that is an event handler and passed in like "<strong>(SEL)selector</strong>".</p>
  
  <p><strong>Custom method</strong> (belongs to a helper class):</p>
  
  <pre><code>+(UIButton*)kNSunSetupWithSelector:(SEL)selector    withX:(int)x y:(int)y width:(int)width height:(int)height
                         buttonTitle:(NSString*)buttonTitle backGroundColor:(CGColorRef) backGroundColor
  {
  
      UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  
      [button setTitle:@"Save" forState:UIControlStateNormal];
      button.frame = CGRectMake(x, y, width, height);
      button.backgroundColor = [UIColor greenColor];
  
  // selector is used over here
      [button addTarget:self
                      action:selector
            forControlEvents:UIControlEventTouchDown];
  
      return button;
  }
  </code></pre>
  
  <p>And then in a <strong>view controller .m file</strong>, I call that custom method like:</p>
  
  <pre><code>-(void)setUpButtons
  {
              SEL selector = @selector(buttonSavePressed:);
              _buttonSave = [KNSunDynamicUIButton kNSunSetupWithSelector:selector withX:470 y:410 width:160 height:40 buttonTitle:@"Save" backGroundColor:(_buttonSaveColor)];
  
              [self.view addSubview:_buttonSave];
  
  }
  
      @interface MyViewController ()
      {
      ...
  
          // buttons
          UIButton* _buttonSave;
      }
      @end
  </code></pre>
  
  <p>And the definition of button's event handler in <strong>the same view controller .m file</strong> like:</p>
  
  <pre><code>- (void)buttonSavePressed:(UIButton*)button
  {
      NSLog(@"Button Save clicked.");
  }
  </code></pre>
  
  <p>When I run my code and hit over the button, I see the exception as mentioned above. Please help thank you.</p>
  
  <p>P.S.  If I rewrite the custom method as an alternative that does not have "(SEL)selector" parameter in its signature, and let controller view, who calls that custom method, does the job of coding selector, then there is <strong>no</strong> exception:</p>
  
  <pre><code>-(void)setUpButtons
  {
      //Note: the codes are in my view controller .m file
  
  _buttonSave = [KNSunDynamicUIButton kNSunSetupWithX:470 y:410 width:160 height:40 buttonTitle:@"Save" backGroundColor:_buttonSaveColor];
  
  
     // Note: selector coding is taken care by codes of my view controller instead of by custom method
     [_buttonSave addTarget:self
      action:@selector(buttonSavePressed:)
      forControlEvents:UIControlEventTouchDown];
  
  
  
      [self.view addSubview:_buttonSave];
      [_buttonSave setupView];
  }
  </code></pre>
  
  <p>And the alternative custom method (I do not like the method because it does not take care of dynamic passed-in selector):</p>
  
  <pre><code>+(UIButton*)kNSunSetupWithX:(int)x y:(int)y width:(int)width height:(int)height
                     buttonTitle:(NSString*)buttonTitle backGroundColor:(CGColorRef) backGroundColor
  {
      UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  
      [button setTitle:@"Save" forState:UIControlStateNormal];
      button.frame = CGRectMake(x, y, width, height);
      button.backgroundColor = [UIColor greenColor];
  
      return button;
  }
  </code></pre>