0
votes

I am trying to assign a delegate's method to a UIButton using addTarget:action:forControlEvents:. Everything compiles without warnings, the IBOutlet is connected to the button in Interface Bulder (XCode 4). If I moves the delegate's method to the controller, it works fine. (All code worked fine, but I refactored to use a delegate, it's my first try with delegates and protocols.)

(added) The protocol declaration, placed before @interface in the .h:

@protocol MMGLVDelegate<NSObject>
  -(void)receiveQuitRequest:(id)sender;
@end

In the controller interface, these properties:

@property (nonatomic, assign) id<TheDelegateProtocol> delegate;
@property (nonatomic, retain) IBOutlet UIButton *quitBtn;

In the controller implementation:

-(void)setDelegate:(id<MMGLVDelegate>)delegate {
  DLog(@"MMGLVSegmented setDelegate: Entered");
  _delegate = delegate;

  [self.quitBtn addTarget:self.delegate action:@selector(receiveQuitRequest:) 
                                   forControlEvents:UIControlEventTouchUpInside];
}

Any help appreciated. Changing target to any of self.delegate, _delegate, or delegate doesn't change app behavior.

What I'm hoping to do is not have to declare a class receiveQuitRequest: that then passes off to the delegate, I'd rather go straight to the delegate from the control.

3
is it accessory to call only delegate method on button click ? or some how to call it ? - Maulik

3 Answers

2
votes

I think you should write

[self.quitBtn addTarget:delegate action:@selector(receiveQuitRequest:) 
                                   forControlEvents:UIControlEventTouchUpInside];

I am not sure, but this may work in your case

0
votes

I have a couple of minor suggestions:

  1. Try disconnecting and re-connecting the delegate for the button in IB. I've noticed that sometimes this seems to reset it properly.

  2. Clean and rebuild. Again, this helps to reset things that didn't get set properly.

0
votes

If I've understood everything correctly, the UIButton does not include a receiveQuitRequest: selector, so there is nothing to be executed when the user touches the button.