Game Development Reference
In-Depth Information
Listing 3-5. GameController.m (Navigate Back to the Welcome View)
- (IBAction)aboutDoneClicked:(id)sender {
[UIView beginAnimations:nil context:@"flipTransitionToBack"];
[UIView setAnimationDuration:1.2];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[aboutView removeFromSuperview];
[self.view addSubview: welcomeView];
[UIView commitAnimations];
}
- (IBAction)highscoreDoneClicked:(id)sender {
[UIView beginAnimations:nil context:@"flipTransitionToBack"];
[UIView setAnimationDuration:1.2];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[highscoreController.view removeFromSuperview];
[self.view addSubview: welcomeView];
[UIView commitAnimations];
real difference is that we remove the About and High Score views and then add the Welcome view.
This is a reversal of the tasks in Listing 3-4.
When the user navigates to the Play view, they stay on that view until their game is done. This
application is set up to automatically change views in this case. The next section explains how
GameController interacts with the CoinsController , not only to know when to transition the user to
the High Score view but also when to update the remaining turns and score labels.
Using a Delegate to Communicate Application State
A common pattern for one object to communicate with another in iOS development is the delegate
pattern. A delegate is simply any object that conforms to some predefined protocol that another
object knows about. For example, say we have class A and it has a property called delegate that
must conform to the protocol P. We can assign an instance of class B to the delegate property of
A, if class B conforms to the protocol P. The delegate pattern is very much like the listener pattern
found in other languages, like Java.
Declaring CoinsControllerDelegate
In order for our CoinsController class to communicate its state with the class GameController , we
create a protocol that describes this relationship. Listing 3-6 shows the declaration of the protocol
CoinsControllerDelegate found in the file CoinsController.h .
 
Search WWH ::




Custom Search