In Objective-C, protocol is simply a name for the groups of methods. A protocol doesn't have instance variables, and it doesn't implement any methods it declares. It just says, any object that conforms to this protocol must implement methods X, Y, and Z.
i.g.
Object A, and B
make A the delegate of B, so
A <XXDelegate>, in B, we declare id <XXDelegate> delegate; and assign delegate = a.
.h
@property (weak,nonatomic)id <AddItemViewControlDelegate> delegate;
.m
- (void)Cancel
{
[self.delegateaddItemViewControlDidCancel:self];
}
- (void)Done
{
ChecklistsItem *item = [[ChecklistsItemalloc]init];
item.text =self.textField.text; //!!Get the text from UI
item.checked = NO;
//close the window . let the delegate do it
[self.delegateaddItemViewControlDidDone:selfdidFinishAddingItem:item];
}
Set the delegate pp125
In A's delegate method, when we have reference to B, we will set like this:
b.delegate = self;
Set delegate in delegate
Any time you want one part of your app to notify another part about something, usually in order to update the screen, you want to use delegate. It's the iOS way.
//invoked by UIKit when a segue from one screen to another is about to be performed
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifierisEqualToString:@"AddItem"]) {
UINavigationController *navigationController = segue.destinationViewController;
AddItemViewController *controller = (AddItemViewController *)[navigationControllertopViewController];
controller.delegate = self;
}
}