Reputation: 24426
I've got a UIViewController
called "MainViewController" which uses a custom class 'CustomController' to control one of it's sub-views. The custom class instantiates UIButton
s in code. How would I get these buttons to trigger methods in the MainViewController?
Upvotes: 1
Views: 248
Reputation: 22939
I think delegation is the way to go there.
Define the protocol CustomControllerDelegate
inside CustomController.h
with a method like this for example:
- (void) customControllerButtonPressed(id)sender; // BTW: you can use `CustomController` instead of `id` if you make a forward declaration for this class
Add a delegate property and synthesize it in the .m
file
@property (assign) id<CustomControllerDelgate> delegate;
Now when your button is pressed you simply call the delegate:
[self.delegate customControllerButtonPressed:self];
In your MainViewController
you make it conform to the specified protocol and set the CustomControllers delegate like so:
CustomViewController *customVC = [[CustomViewController alloc] init];
customVC.delegate = self;
Now when the button is pressed, the MainViewController
's implementation of the specified method is called.
Upvotes: 3