开发者

Obj C delegate methods not assigned to UIButton (iOS)?

开发者 https://www.devze.com 2023-03-20 18:43 出处:网络
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 (XC

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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消