What is the difference between Delegate and Notification?
I understood like delegate and protocol,
@protocol classADelegate
-(void)DelegateMethod;
Simply We can say,
Delegates:
One - to - One
Notification:
One - to - Many
To declare Delegate
@protocol DelegateName
@required
- (void)method:(NSString *)param;
@optional
- (void)methodOptional:(NSString *)param;
@end
And declare a property for Protocol
@property id delegate;
You can use
myObject.delegate = <# some object conforming to DelegateName #>;
NSNotification declaration
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notificationHappened:)
name:MyCustomNotificationName
object:nil];
Then Implement
- (void)notificationHappened:(NSNotification *)notification {
// do work here
}
You can post Notification from anywhere using,
[[NSNotificationCenter defaultCenter] postNotificationName:MyCustomNotificationName
object:self
userInfo:nil];
call removeObserver:
when you are finished.