210 likes | 341 Views
Chapter 6 Flag Quiz Game App. More IB: UISegmentedControl, UISwitch, images Using a Utility template that provides skeleton code supporting 2 views: a front side view and a flip side view Memory Management. Utility Template.
E N D
Chapter 6 Flag Quiz Game App • More IB: UISegmentedControl, UISwitch, images • Using a Utility template that provides skeleton code supporting 2 views: a front side view and a flip side view • Memory Management
Utility Template • The Utility template provides several skeleton classes with some functionality already implemented to support and to go back and forth between 2 views: • The frontside view (the game) • The flipside view (the settings of the game: easy, average, difficult, ..)
Utility Template • Open a project choosing the Utility template • Build and Run • Flip back and forth between the 2 views using the info and done button • Let’s look at the info button
Utility Template • Open the Main_iPhone storyboard file • Click on the info button • Open the Attributes Inspector • Type of button is Info Light (UIButtonTypeInfoLight value of UIButtonType)
MainViewController.h • Imports FlipsideViewController.h • Extends UIViewController • Implements FlipsideViewControllerDelegate protocol (inside angle brackets) and UIPopoverControllerDelegate • Protocol = similar to interface in Java (a way to implement multiple inheritance)
Protocol • Syntax: • @interface SubclassName : SuperclassName <protocolName1[, protocolName2, ..]> • @interface A : B <C, D, E> • Class A extends class B and implements protocols C, D and E
FlipsideViewController.h • FlipsideViewControllerDelegate protocol is declared inside FlipSideViewController.h • It has one method: flipsideViewControllerDidFinish: • That method, implemented in MainViewController.m, returns the app to the main view when the user touches the Done button in the flip side view
FlipsideViewController.h • Why is flipsideViewControllerDidFinish: implemented in MainViewController.m? • Because MainViewController implements the FlipsideViewControllerDelegate protocol
Delegate • Has a property, named delegate, of type id (generic object) that implements the FlipsideViewControllerDelegate • Inside the class, we can access it via self.delegate • It represents the MainViewController object controlling the front view
Delegate • We can use it to call flipsideViewControllerDidFinish: • We can type cast it to be a MainViewController object • it enables communication between the 2 views
View Transitions • Look at the done method (in FlipsideViewController.m): It makes the app go from the flip side to the front side • Look at the Main_iPhone storyboard file, click on the info button, open the Connections Inspector: this makes the app go from the front side to the flip side
done method • Using the delegate, it calls the flipsideViewControllerDidFinish method • If the device is an iPhone, that method calls dismissViewControllerAnimated:completion: (of the class UIViewController); that returns the app to the main view
Info button • Connections inspector Triggered Segues • Says “Modal”, FlipSide View Controller • When the button is clicked, a View controlled by the FlipsideViewController will be on top of the View stack • Also, the prepareForSegue:sender: method (of the UIViewController class) executes (it is called automatically)
Info button • A segue (UIStoryboardSegue class) is a transition between 2 View Controllers; it is created by the storyboard runtime • If we want to initiate a segue by code, we can: we call the performSegueWithIdentifier:sender: method
Info button • We could write our own method and link it to a “Touch Up Inside” event on the Info button • It would create a FlipsideViewController object and push its View to the top of the stack
Views • Views are managed in a stack: • Present a view add view on top of the stack • Dismiss a view remove view from the stack • The view at the top of the stack is the one that the user sees
Front side and Flip side • Next: need to pass information between the views (back and forth) • In the front side view, we will have a Model object as an instance variable • Via the delegate, the Flipside view will have access to that Model object
Front side and Flip side • When things change on the flip side, we will update the Model object • Then when we go back to the front side, we will be able to update the view accordingly • Essentially, the Model object is accessible from both views
Updating the front side • Inside the flip side view controller, we access the delegate like this: (MainViewController *) self.delegate • And with it we have access to the Model object
iPad • This skeleton also applies to the iPad • Main_iPad storyboard • Only “one” View • The flip side View is a popover View • Visible when user clicks on Info • Dismissed when the user clicks on Done
Memory Management • Our code is getting more and more complex, and we have objects everywhere, several views that come and go, .. • We need to look at how memory is managed