Say I have a custom container view controller (MainViewController) where I do something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
You can either use delegate or block;
Using delegate
Create a protocol :
@protocol SomeProtocol
- (void)someAction;
@end
Just declare a delegate in HomeViewController.h like this:
id delegate;
and then in MainViewController's viewDidLoad set it like this:
homeVC.delegate = self;
//some where in MainViewController implement the protocol method
-(void)someAction
{
//do something
}
then when you press the button in homeVC, just simply call:
if ([self.delegate respondsToSelector:@selector(someAction)]) {
[self.delegate someAction];
}
Using Block:
In HomeViewController.h declare a block property:
typedef void (^ActionBlock)();
@property (nonatomic, copy) ActionBlock block;
then in MainViewController ViewDidLoad:
homeVC.block = ^(){
//do something
};
then when you press the button in homeVC, just simply call:
self.block();