Note: No client code is required for an event handler as it creates only API functionality and cannot be accessed by the client directly. However, the data that is created in the TopCeleb resource can be accessed by the client.
Since the TopCeleb resource uses the Celebrtiy object type, there is no need to define another class in the client code, To use it in the application, we need to add a new Scene, and then populate it with data that is created for us by the event handler in TopCeleb.
First, we add a new File using the UIView Controller subclass template.
Will call this UIViewController TopCelebViewController
Next, we add a new UIViewController to the MainStoryboard by dragging and dropping a View Controller from the Objects library. We set the Custom Class for this UIViewController to the new TopCelebViewController class that we have created.
We then click on the Tab Bar Controller and connect a new Relationship under the Story Board Segues to the TopCelebViewController.
We can now add the UI components we need to this scene. We will add the following:
When you are done, the scene should look like this...
We can now complete the TopCelebViewController implementation. First, we will add the following imports to TopCelebViewController.h:
#import "Celebrity.h"
#import "AppDelegate.h"
Now, we add the following properties:
@property (strong, nonatomic) Celebrity *topCeleb;
@property (nonatomic, retain) IBOutlet UIImageView *topCelebImageView;
@property (nonatomic, retain) IBOutlet UILabel *celebrityLabel;
@property (nonatomic, retain) IBOutlet UILabel *selectedLabel;
@property (nonatomic, retain) IBOutlet UILabel *rejectedLabel;
In addition to the UI components that we will be using, we also add a Celebrity object (topCeleb) to this class that we will populate from the TopCeleb resource on the API.
Now, to the TopCelebViewController.m file, we synthesize the properties:
@synthesize selectedLabel, rejectedLabel, topCeleb, celebrityLabel, topCelebImageView;
We want to retrieve the latest topCeleb object, so we create a getTopCelebrity method:
-(void) getTopCelebrity {
NSError *error;
topCeleb = [[FatFractal main] getObjFromUrl:@"/ff/resources/TopCelebrity?start=0&count=1" error:&error];
if (error) {
NSLog(@"TopCelebViewController getTopCelebrity failed: %@", [error localizedDescription]);
celebrityLabel.text = @"No Top Celebrity found";
return;
} else {
topCelebImageView.image = [[UIImage alloc] initWithData:topCeleb.imageData];
celebrityLabel.text = [NSString stringWithFormat:@"%@ %@", topCeleb.firstName, topCeleb.lastName];
}
}
Then, we need to initialize the properties in the viewDidLoad method and call getTopCelebrity.
- (void)viewDidLoad {
[super viewDidLoad];
topCeleb = [[Celebrity alloc]init];
celebrityLabel.text = nil;
selectedLabel.text = nil;
rejectedLabel.text = nil;
topCelebImageView = nil;
[self getTopCelebrity];
}
And clear them on an unload:
- (void)viewDidUnload {
[super viewDidUnload];
topCeleb = nil;
celebrityLabel.text = nil;
selectedLabel.text = nil;
rejectedLabel.text = nil;
topCelebImageView = nil;
}
Lastly, make sure and connect up your UIComponents in the MainStoryboard.storyboard for the TopCelebViewController elements.
celebrityLabel
Referencing Outlet to TopCelebViewController/celebrityLabel
selectedLabel
Referencing Outlet to TopCelebViewController/selectedLabel
rejectedLabel
Referencing Outlet to TopCelebViewController/rejectedLabel
topCelebImageView
Referencing Outlet to TopCelebViewController/topCelebImageView
Now, as soon as ratings are being captured in WouldYa’s, then the topCeleb will be populated by the event handler and can be viewed in this scene.
Final result - successfully created a TopCelebrity on the API from an event handler. We have also added a scene to display the results.