Game Development Reference
In-Depth Information
HighscoreController: A Simple, Reusable Component
The HighscoreController class is responsible for managing a view for displaying high scores as well
as persisting those high scores. The HighscoreController is a pretty simple class—the header file
for the class is shown in Listing 3-8.
Listing 3-8. HighscoreController.h
#define KEY_HIGHSCORES @"KEY_HIGHSCORES"
#import<UIKit/UIKit.h>
#import "Highscores.h"
#import "Score.h"
@interface HighscoreController : UIViewController {
IBOutlet UIView* highscoresView;
Highscores* highscores;
}
-(void)saveHighscores;
-(void)layoutScores:(Score*)latestScore;
-(void)addScore:(Score*)newScore;
@end
As can be seen in Listing 3-8, HighscoreController has two fields. The first field, highscoresView ,
is an IBoutlet . This is the UIView that will be used to lay out the actual scores on the screen. The
UIView highscoresView is assumed to be a sub-view of the view property that comes with the class
UIViewController . It does not have to be a direct sub-view—it just has to appear somewhere in the
view hierarchy. This is a slightly different pattern from other UIViewControllers we have seen. It was
done this way so an instance of HighscoreController could be added to both the iPhone and iPad
XIB files, and the layout of the Views could be controlled in there. Inspect the iPhone XIB file and you
will see that the layout for the High Score view is defined directly within the HighscoreController .
The field highscores is of type Highscores . This is a simple class that contains an array of Score
objects. We will take a closer look at the classes Highscores and Score after looking at the
implementation of the three tasks defined in HighscoreController .
HighscoreController Implementation and Layout
Once an application has the views for HighscoreController wired up, the most important task is
the addScore: task. This task is called when a game is over and the application should update the
High Score view, as well as make sure the high score information is stored. Listing 3-9 shows the
implementation of addScore: .
Listing 3-9. HighscoreController.m (addScore:)
-(void)addScore:(Score*)newScore{
[highscores addScore:newScore];
[self saveHighscores];
[self layoutScores: newScore];
}
 
Search WWH ::




Custom Search