Suppose you have two views ViewA
and ViewB
Instance of ViewB
is created inside ViewA
, so ViewA
can send message to ViewB's
instance, but for the reverse to happen we need to implement delegation (so that using delegate ViewB's
instance could send message to ViewA
)
Follow these steps to implement the delegation
ViewB
create protocol as@protocol ViewBDelegate
-(void) exampleDelegateMethod;
@end
@interface ViewB : UIView
@property (nonatomic, weak) id< ViewBDelegate > delegate;
@end
@interfac ViewA: UIView < ViewBDelegate >
-(void) anyFunction
{
// create Class ViewB's instance and set the delegate
[viewB setDelegate:self];
}
ViewA
-(void) exampleDelegateMethod
{
// will be called by Class ViewB's instance
}
ViewB
to call the delegate method as-(void) callDelegateMethod
{
[delegate exampleDelegateMethod];
//assuming the delegate is assigned otherwise error
}