1) Protocol definition
Given protocol in DataModel.h, we have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@protocol UpdateViewDelegate <NSObject> @optional -(void)showMessageBox:(NSString*)title andMessage:(NSString*)msg andCancelTitle:(NSString*)cancelTitle; // for register view controller -(void)addFacesToCarousel; -(void)clearFacesInCarousel; -(void)updateNumOfFacesLabel:(NSString*)strNumber; // for login view controller -(void)animateTraining:(bool)flag; -(void)incrementIdentityValue:(float)similarity; -(void)showSimilarity:(float)similarity; -(void)setRecognizeButton:(bool)flag; -(void)setTrainButton:(bool)flag; -(void)showIdentityInt:(int)identity andName:(NSString*)name; @end |
2) Define the protocol delegate in delegator (source) class
Our DataModel class is the delegator. It is the “source” of all of our delegations.
1 2 3 |
@interface DataModel : NSObject { id <UpdateViewDelegate> delegate; } |
the delegate means it “delegates” the messages sent by DataModel (source) to whatever “view” class/controllers (destination) that the delegates point to. Hence, that “view” class/controller will have to implement the delegate methods(s).
3) Implement protocol method in delegator(source) class
In DataModel.m, we have:
1 2 3 4 5 6 |
-(void)showMessageOnDelegateView:(NSString*)title andMessage:(NSString*)message andCancelTitle:(NSString*)cancelTitle{ if([self.delegate respondsToSelector:@selector(showMessageBox:andMessage:andCancelTitle:)]) { [delegate showMessageBox:title andMessage:message andCancelTitle:cancelTitle]; } } |
..this means, whatever object responds (or has the implementation) to what our delegate has asked for in the protocol declaration, we make that object take care of it.
4)Conform to the delegate in view/controller(delegator) class
Hence, let’s say we have RegistrationViewController. We make it conformed to our delegate UpdateViewDelegate like so:
1 2 3 4 |
@interface RegistrationViewController : UIViewController <UpdateViewDelegate> { } |
Which means this class (RegistrationViewController) will have to implement the delegate methods here.
And since our UpdateViewDelegate are all optional methods, we can optionally implement those methods. In our example, let’s implement showMessageBox:
1 2 3 4 5 6 7 8 9 10 11 12 |
#pragma mark - UpdateViewDelegate delegate methods -(void)showMessageBox:(NSString*)title andMessage:(NSString*)msg andCancelTitle:(NSString*)cancelTitle { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertView * warn = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:nil]; [warn show]; [warn release]; }); } |
However, those implemented UpdateViewDelegate methods won’t be of any use because if no delegate messages gets passed here. Hence, we will need to have other classes that conforms to UpdateViewDelegate to pass their delegates.
We do so by using those classes (delegator) in our RegistrationViewController (delegatee):
We use the DataModel (which conforms to UpdateViewDelegate) object in our class:
1 2 3 4 |
@interface RegistrationViewController : UIViewController <UpdateViewDelegate> { DataModel * registerDataModel; } |
Then assign the delegate from our DataModel object to self.
1 2 3 4 5 6 7 8 |
-(id)initWithCamera:(CameraWrapper*)newCamera { if(self=[self init]) { //the data model self.registerDataModel = [[[DataModel alloc] init] autorelease]; self.registerDataModel.delegate = self; } return self; } |
Now, whenever DataModel object has delegated messages to our RegistrationViewController, the RegistrationViewController’s protocol methods will be able to take care of it.