Created
March 12, 2014 11:11
-
-
Save keicoder/9504854 to your computer and use it in GitHub Desktop.
objective-c : Basic delegate pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Basic delegate pattern | |
//AppDelegate.h | |
@interface AppDelegate : UIResponder <UIApplicationDelegate> | |
@property (strong, nonatomic) UIWindow *window; | |
@end | |
//AppDelegate.m | |
@implementation AppDelegate | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
return YES; | |
} | |
@end | |
//AddDelegateViewController.h | |
@protocol AddDelegateViewControllerDelegate; | |
@interface AddDelegateViewController : UIViewController | |
// AddViewControllerDelegate | |
@property (nonatomic, weak) id <AddDelegateViewControllerDelegate> delegate; | |
@end | |
// Protocol definition starts here | |
@protocol AddDelegateViewControllerDelegate <NSObject> | |
@required | |
- (void) addDelegateViewControllerDidSave; | |
- (void) addDelegateViewControllerDidCancel; | |
@end | |
// Protocol Definition ends here | |
//AddDelegateViewController.m | |
@implementation AddDelegateViewController | |
//Cancel | |
- (IBAction)addViewCancelButtonPressed:(UIBarButtonItem *)sender { | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
if (self.delegate && [self.delegate respondsToSelector:@selector(addDelegateViewControllerDidCancel)]) { | |
[self.delegate addDelegateViewControllerDidCancel]; | |
} | |
} | |
//Save | |
- (IBAction)addViewSaveButtonPressed:(UIBarButtonItem *)sender { | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
if (self.delegate && [self.delegate respondsToSelector:@selector(addDelegateViewControllerDidSave)]) { | |
[self.delegate addDelegateViewControllerDidSave]; | |
} | |
} | |
@end | |
//DelegateTableViewController.h | |
#import "AddDelegateViewController.h" // needed to include the @delegate protocol info | |
@interface DelegateTableViewController : UITableViewController <AddDelegateViewControllerDelegate> | |
@property (nonatomic, strong) AddDelegateViewController *AddDelegateViewController; | |
- (void)addDelegateViewControllerDidCancel; | |
- (void)addDelegateViewControllerDidSave; | |
@end | |
//DelegateTableViewController.m | |
@implementation DelegateTableViewController | |
#pragma mark - Navigation | |
// In a storyboard-based application, you will often want to do a little preparation before navigation | |
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
if ([[segue identifier] isEqualToString:@"toAddView"]) { | |
//ex | |
//1. to typical view controller with modal segue | |
//AddDelegateViewController *advc = (AddDelegateViewController *)[segue destinationViewController]; | |
//2. embed in navigation controller with modal segue | |
UINavigationController *nav = [segue destinationViewController]; | |
AddDelegateViewController *advc = (AddDelegateViewController *)[nav topViewController]; | |
advc.delegate = self; | |
} | |
} | |
- (void)addDelegateViewControllerDidCancel | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
[self dismissViewControllerAnimated:YES completion:^{ | |
NSLog(@"Dismiss Complete!"); | |
}]; | |
} | |
- (void)addDelegateViewControllerDidSave | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
[self dismissViewControllerAnimated:YES completion:^{ | |
NSLog(@"Dismiss Complete!"); | |
}]; | |
} | |
#pragma mark - Table view data source | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
{ | |
return 1; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
return 5; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; | |
// Configure the cell... | |
return cell; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment