One option is to set the button up using
[myButton addTarget:yourOtherClass action:@selector(mySelector:) forControlEvents:UIControlEventTouchUpInside];
but this is a bit dangerous because target
is not retained so you could send the message to a deallocated object.
You could instead set up a protocol
MyController.h
@protocol MyControllerDelegate - (void)myController:(MyController *)controller buttonTapped:(UIButton *)button; @end @interface MyController : UIViewController @property (nonatomic, assign) id <MyControllerDelegate> delegate; - (IBAction)buttonTapped:(UIButton *)button; @end
Then in your implementation
MyController.m
- (IBAction)buttonTapped:(UIButton *)button { if ([self.delegate respondsToSelector:@selector(myController:buttonTapped:)]) { [self.delegate myController:self buttonTapped:button]; } }
As the method defined in the protocol was not optional I could have instead done a check for (self.delegate)
to make sure it is set instead of respondsToSelector
.