Game Development Reference
In-Depth Information
Using the
RockPaperScissorsView
from Chapter 1, let's take a look at how this functionality
would appear if it were implemented as a
UIViewController
. Listing 2-3 shows the file
RockPaperScissorsController.h
.
Listing 2-3. RockPaperScissorsController.h
@interface RockPaperScissorsController : UIViewController {
UIView* buttonView;
UIButton* rockButton;
UIButton* paperButton;
UIButton* scissersButton;
UIView* resultView;
UILabel* resultLabel;
UIButton* continueButton;
BOOL isSetup;
}
-(void)setup:(CGSize)size;
-(void)userSelected:(id)sender;
-(void)continueGame:(id)sender;
-(NSString*)getLostTo:(NSString*)selection;
-(NSString*)getWonTo:(NSString*)selection;
@end
In Listing 2-3, we see the class
RockPaperScissorsController
extends
UIViewController
. Among
other things, this means that
RockPaperScissorsController
has a property called "
view
" that will be
the root
UIView
for this controller. Like
RockPaperScissorsView
, we will have other
UIViews
that are
subviews of the root
view
, such as the buttons for selecting your choice. Although these buttons
could conceivably have their own
UIViewControllers
, there comes a point when it makes sense to
allow a
UIViewController
to manage all of the
UIViews
with which it is concerned. There are very
few changes required on the implementation side of things to complete this transition from
UIView
to
UIViewController
. Basically, wherever we used the keyword "
self
," we simply say
self.view
instead. Listing 2-4 shows the required changes.
Listing 2-4. RockPaperScissorsController.m (Partial)
-(void)setup:(CGSize)size{
if (!isSetup){
isSetup = true;
srand(time(NULL));
buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
[buttonView setBackgroundColor:[UIColor lightGrayColor]];
[
self.view
addSubview:buttonView];