Generic Objective-C Delegation template


/ Published in: Objective C
Save to your folder(s)

Delegate is the object that are listning to Delegatior.


Copy this code and paste it in your HTML
  1. // Delegatior.h
  2.  
  3. #import <Foundation/Foundation.h>
  4.  
  5. @protocol SaveActionDelegate;
  6.  
  7. @interface Delegatior : NSObject {
  8. id <SaveActionDelegate> delegate;
  9. }
  10.  
  11. @property (nonatomic, assign) id <SaveActionDelegate> delegate;
  12.  
  13. - (IBAction)cancel:(id)sender;
  14. - (IBAction)save:(id)sender;
  15.  
  16. @end
  17.  
  18. @protocol SaveActionDelegate
  19. - (void)delegationObject:(Delegatior *)obj didSave:(BOOL)save;
  20. @end
  21.  
  22. // Delegatior.m
  23.  
  24. #import "Delegatior.h"
  25.  
  26. @implementation Delegatior
  27.  
  28. @synthesize delegate;
  29.  
  30. - (IBAction)cancel:(id)sender {
  31. [delegate delegationObject:self didFinishWithSave:NO];
  32. }
  33.  
  34. - (IBAction)save:(id)sender {
  35. [delegate delegationObject:self didFinishWithSave:YES];
  36. }
  37.  
  38. - (void)dealloc {
  39. [delegate release];
  40. [super dealloc];
  41. }
  42.  
  43. @end
  44.  
  45. // Delegate.h
  46. #import <Foundation/Foundation.h>
  47. #import "Delegatior.h"
  48.  
  49. @interface Delegate : NSObject <SaveActionDelegate> {
  50.  
  51. }
  52.  
  53. - (void)delegationObject:(Delegatior *)obj didSave:(BOOL)save;
  54.  
  55. @end
  56.  
  57. // Delegate.m
  58. #import "Delegate.h"
  59.  
  60. @implementation Delegate
  61.  
  62. - (void)viewDidLoad {
  63. [super viewDidLoad];
  64. Delegatior *delegatior = [[Delegatior alloc] init];
  65. delegatior.delegate = self;
  66. }
  67.  
  68. - (void)delegationObject:(Delegatior *)obj didSave:(BOOL)save {
  69. [self doStuff];
  70. }
  71.  
  72. @end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.