I'm hoping Brian will dive in here with some clarification if I go seriously astray with this, but I decided to respond to this Q as an exercise.
Disclaimer: I know absolutely nothing about Objective C. At all.
I am testing the theory that by knowing PureMVC and having an already known demo ported to study, I should be able to begin understanding things about an unfamiliar language rather quickly.
Fortunately Brian has ported the original Flex Employee Admin Demo
1 to the iPhone
2, and even though the form factor is somewhat different, he has done an admirable job of keeping the actors and collaborations the same. So even though ObjectiveC looks pretty strange to me, I spent about 20 minutes looking through the code (and googling to find out the details of Objective C protocols and view controller delegates) here's what I gleaned:
Firstly, it appears that events as we know them in AS3 do not exist. Instead, what is used is a 'View-Controller Delegate'. This allows the view to talk to its controller, whatever it may be. The view component is given a reference to its delegate in order to communicate with the rest of the system. Surprise! This turns out to be your PureMVC Mediator. The Mediator must therefore not only act as a classic PureMVC Mediator, but also expose a set of methods for the component to invoke when it needs to communicate with the system (in lieu of dispatching events). So...
UserForm.h defines a UserFormViewControllerDelegate protocol which defines the properties and methods to be exposed by said delegate.
http://trac.puremvc.org/Demo_ObjectiveC_UIKit_EmployeeAdmin/browser/tags/EmployeeAdmin_1_0/src/org/puremvc/objectivec/demos/uikit/employeeadmin/view/components/UserForm.hNote the statements:
-(void)createUserSelected:(UserVO *)userVO;
-(void)updateUserSelected:(UserVO *)userVO;
-(void)userRolesSelected:(UserVO *)userVO;
In a way, these are defining your 'events' by way of declaring the methods that this component's delegate must expose. Your mediator ends up implementing these methods in the same way AS3 would implement event listeners.
UserForm.m defines a UserForm implementation. This is your view component. http://trac.puremvc.org/Demo_ObjectiveC_UIKit_EmployeeAdmin/browser/tags/EmployeeAdmin_1_0/src/org/puremvc/objectivec/demos/uikit/employeeadmin/view/components/UserForm.mNotice lines for instance in the saveUser method:
if (mode == NEW) {
[delegate createUserSelected:userVO];
} else if (mode == EDIT) {
[delegate updateUserSelected:userVO];
}
These are 'dispatching' the 'events' (i.e. calling the methods on its ViewControllerDelegate, which ends up being your Mediator).
UserFormMediator.h declares the interface for UserFormMediator.http://trac.puremvc.org/Demo_ObjectiveC_UIKit_EmployeeAdmin/browser/tags/EmployeeAdmin_1_0/src/org/puremvc/objectivec/demos/uikit/employeeadmin/view/UserFormMediator.hNotice the angle brackets specifying UserFormMediator adopt the protocol declared in UserFormViewControllerDelegate.
@interface UserFormMediator : Mediator <UserFormViewControllerDelegate> {
}
This says that UserFormMediator will subclass Mediator, but will also implement UserFormViewControllerDelegate's interface.
UserFormMediator.m defines the implementation for the UserFormMediator
http://trac.puremvc.org/Demo_ObjectiveC_UIKit_EmployeeAdmin/browser/tags/EmployeeAdmin_1_0/src/org/puremvc/objectivec/demos/uikit/employeeadmin/view/UserFormMediator.mNotice that in its onRegister method, the view component is given a reference to its delegate, the mediator:
-(void)onRegister {
self.viewComponent.delegate = self;
}
Also notice it implements the methods defined in the UserFormViewControllerDelegate:
-(void)createUserSelected:(UserVO *)userVO {
[self sendNotification:CreateUser body:userVO];
}
-(void)updateUserSelected:(UserVO *)userVO {
[self sendNotification:UpdateUser body:userVO];
}
So when the
UserForm does:
[delegate createUserSelected:userVO];
This method is called on your
UserFormMediator-(void)createUserSelected:(UserVO *)userVO {
[self sendNotification:CreateUser body:userVO];
}
So, for those aware of the best practices for PureMVC, the first thing you'll probably say is: 'whoah! view components aren't supposed to know about their Mediators! Giving the view component a reference to the Mediator breaks this practice into tiny pieces and jumps up and down on them doesn't it?
Actually no. No where in the definition of UserForm is UserFormMediator referenced. It is given an instance of some object that implements UserFormViewControllerDelegate. It knows only about the protocol exposed by that object; specifically the methods that we would otherwise implement as event handlers. Mediator specific things like 'handleNotification' or 'onRemove' can't be reached.
The ViewControllerDelegate protocol is essentially declaring: 'these are the things you can say to the system' through me. In AS3 we would say these things by dispatching events, which would be listened for and end up invoking these same methods in the Mediator.
IMHO, it's actually sort of nice to see the formalization of the communications protocol from the view component to the mediator done in this way. In fact it immediately makes me think that for FlashLite versions where there are no Events, this would be quite a nice pattern to implement when using PureMVC.
-=Cliff>
1AS3/Flex Employee Admin Demo:
http://trac.puremvc.org/Demo_AS3_Flex_EmployeeAdmin2Objective C/UIKit Employee Admin Demo:
http://trac.puremvc.orgDemo_ObjectiveC_UIKit_EmployeeAdmin