Skip to content

Instantly share code, notes, and snippets.

@keicoder
Last active January 1, 2016 06:49
Show Gist options
  • Save keicoder/8107251 to your computer and use it in GitHub Desktop.
Save keicoder/8107251 to your computer and use it in GitHub Desktop.
objective-c : 기본적인 delegate pattern
기본적인 delegate pattern
//steps for setting up the delegate pattern between two objects,
//where object A is the delegate for object B and object B will send out the messages:
//1.Define a delegate @protocol for object B.
//2.Give object B a property for that delegate protocol.
//3.Make object B send messages to its delegate when something interesting happens, such as the user pressing the Cancel or Done buttons, or when it needs a piece of information.
//4.Make object A conform to the delegate protocol. It should put the name of the protocol in its @interface line and implement the methods from the protocol.
//5.Tell object B that object A is now its delegate.
//Why use delegates
//delegate helps to abstract the dependency from screen B on screen A.
//delegate protocol defines a contract between screen B and any screens that wish to use it.
//By making it a delegate of screen B, screen A doesn’t have to put any methods in its public @interface beyond those from the delegate protocol.
//Anytime you want one part of your app to notify another part about something, usually in order to update the screen, you want to use delegates. It’s the iOS way.
AddItemViewController.h ->
//At the top of AddItemViewController.h, add in between the #import and @interface
@class AddItemViewController;
@class ChecklistItem; //forward declaration because using delegate protocol, need as a parameter for the method addItemViewController: didFinishAddingItem:
@protocol AddItemViewControllerDelegate <NSObject>
- (void)addItemViewControllerDidCancel:(AddItemViewController *)controller;
- (void)addItemViewController:(AddItemViewController *)controller didFinishAddingItem:(ChecklistItem *)item;
@end
@property (nonatomic, weak) id <AddItemViewControllerDelegate> delegate;
AddItemViewController.m ->
#import "ChecklistItem.h"
@synthesize delegate;
- (IBAction)cancel {
[self.delegate addItemViewControllerDidCancel:self];
}
- (IBAction)done {
ChecklistItem *item = [[ChecklistItem alloc] init];
item.text = self.textField.text;
item.checked = NO;
[self.delegate addItemViewController:self didFinishAddingItem:item];
}
ChecklistsViewController.h ->
#import "AddItemViewController.h"
@interface ChecklistsViewController : UITableViewController <AddItemViewControllerDelegate> //ChecklistsViewController now conforms to the AddItemViewControllerDelegate protocol
ChecklistsViewController.m ->
- (void)addItemViewControllerDidCancel:(AddItemViewController *)controller {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)addItemViewController:(AddItemViewController *)controller didFinishAddingItem:(ChecklistItem *)item {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"AddItem"]) {
UINavigationController *navigationController = segue.destinationViewController;
AddItemViewController *controller = (AddItemViewController *)navigationController.topViewController;
controller.delegate = self;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment