Delegates
Rather than passing the method itself to be called back, another callback strategy is to pass in an object that contains the callback methods. Delegation is such a design pattern, another way that children in Objective C and Swift can communicate with their parent. Delegates formalise the list of messages/methods that a child may callback in something called a protocol. A parent notifies the child what is the child’s ‘delegate’(usually the parent just defines itself as the delegate) and then implements the relevant available methods in the protocol. The delegate will then receive these callbacks from the child.
ActionScript
Delegates aren’t a popular structure in ActionScript but certainly could be followed using Interfaces as protocols. The analogy would be that a child defines a callback class with an interface(the protocol), and then the parent implements that interface and passes itself to the child. In fact the client property in NetStream is a type of delegate, the only difference being that its Interface(protocol) is implicit.
Objective C
Here’s a basic example of how to implement delegates and protocols in Objective C.
The child, let’s call it MyClass:
/// MyClass Header(.h) File @class MyClass; @protocol MyClassDelegate <NSObject> - (void) myClassDelegateMethod: (MyClass *) sender; @end //end protocol @interface MyClass : NSObject { } @property (weak, nonatomic) id <MyClassDelegate> delegate; @end /// MyClass (.m) File #import "MyClass.h" @implementation MyClass @synthesize delegate; - (void) doSomething { //this method gets called by MyClass at some point //does some stuff here [self.delegate myClassDelegateMethod:self]; //calls the MyClassDelegate method to notify the delegate } @end
Now in the parent class, for example a ViewController called MYViewController
//MYViewController.h #import "MyClass.h" @interface MYViewController:UIViewController <MyClassDelegate> { //make it a delegate for MyClassDelegate } //MYViewController.m myClass.delegate = self; //set its delegate to self somewhere //implement delegate method - (void) myClassDelegateMethod: (MyClass *) sender { NSLog(@"MyClass called this method via MyClassDelegate!"); }
Swift
And here is the same example in Swift – much less verbose without header files.
First the child, MyClass:
//MyClass.swift protocol MyClassDelegate { func myClassDelegateMethod(sender:MyClass) } class MyClass { var delegate: MyClassDelegate? func doSomething { delegate?.myClassDelegateMethod(self) } }
Now the parent class, a ViewController we’ve called MYViewController
//MYViewController.swift class MyViewController:UIViewController, MyClassDelegate { var myClass:MyClass init() { myClass = MyClass() myClass.delegate = self myClass.doSomething() } func myClassDelegateMethod(sender:MyClass) { println("MyClass called this method via MyClassDelegate!") } ... }
[…] Part 3: Delegates […]
hi, in the swift implementation of MyViewController you are missing : myClass.delegate = self;
Good pickup – i wasn’t even instantiating MyClass, have rectified!
Watch the muscle memory with those semi-colons!
[…] Part 3: Delegates […]