© Kenneth M. Anderson, 2011INTERMEDIATE IOSCSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGNLECTURE 17 — 03/08/20111Tuesday, March 8, 2011© Kenneth M. Anderson, 2011Goals of the LectureLearn more about iOSIn particular, focusing on the concept of views and their associated view controllersBut also covering: autorelease, @selector, the use of Instruments to track allocations, gesture recognizers, animation, split view controllers & table view controllers!2Tuesday, March 8, 2011© Kenneth M. Anderson, 2011iOS Fundamentals (I)Each iOS application hasone application delegateone windowone or more view controllerseach view controller has one view that typically has many sub-views arranged in a tree structuree.g. views contain panels contain lists contain items… 3Tuesday, March 8, 2011© Kenneth M. Anderson, 2011iOS Application Architecture4sharedApplication()UIApplicationdelegateUIApplicationDelegateUIWindowwindowUIViewControllerrootViewControllerUIViewsubviews*Data structures and resources not shownTuesday, March 8, 2011© Kenneth M. Anderson, 2011iOS Fundamentals (II)A window will have a “root” view controllerSome view controllers allow us to “push” a new view controller onto a stack (similar to Android’s activity stack)the new view controller’s view is then displayedWhen we “pop” that view controller off the stack, we return to the view of the previous view controllerAt other times, we may switch the “root” view controller entirelythe new view is displayed and the previous view controller (and its view) is deallocated5Tuesday, March 8, 2011© Kenneth M. Anderson, 2011iOS Fundamentals (III)View controllers can be instantiated and activated via the use of .xib files (as we saw last time) or they can be created programmaticallyThey, in turn, can create their view through the use of a .xib file or create their view programmaticallyWe’ll see examples of both in this lecture6Tuesday, March 8, 2011© Kenneth M. Anderson, 2011iOS Fundamentals (IV)View controllers are very powerfulthey handle the creation of viewsthey handle navigation among views and other view controllersthey help free up memory when a view is no longer being displayedthey handle the rotation of views when a device’s orientation changes7Tuesday, March 8, 2011© Kenneth M. Anderson, 2011With respect to orientationBack in lecture 13, I had a problem in which my view would not change orientation when I rotated the iPhone simulatorI blame myself for not appeasing the demo gods AND (more importantly) I forgot to edit a method in my view controllerEach view controller can override the following methodshouldAutorotateToInterfaceOrientation:Its default code keeps views in a portrait orientation8Tuesday, March 8, 2011© Kenneth M. Anderson, 2011Default CodeThe default code is commented out in the view controller template; this shows you the default behaviorthis code will only return YES (true) for UIInterfaceOrientationPortraitand thus the view stays in Portrait orientation9DEMOTuesday, March 8, 2011© Kenneth M. Anderson, 2011Say “yes” to changes in orientationIn order to rotate to any orientation, we override the default behavior and return YES to any requestThis is an example of delegation. When we rotate the device, the application asks the current view controller “SHOULD we autorotate to this new orientation”Our change above says “YES!”For most apps, the autorotation will work just fine10DEMOTuesday, March 8, 2011© Kenneth M. Anderson, 2011Simple View-Based ApplicationImage SwitcherView-Based Application TemplateTwo i m a g e v i e w sOne page controllerThe two image views will work with the page controller to make it look like multiple images are available to display11Tuesday, March 8, 2011© Kenneth M. Anderson, 2011Step 1: Add ImagesYo u c a n d o t h i s v i a D r a g a n d D r o p o rProject Add to Project…Te l l X C o d e t o c o py t h e fi l e sYo u w a n t t h e i m a g e s t o e n d u p i n t h e Resources folder of the project view12Tuesday, March 8, 2011© Kenneth M. Anderson, 2011Step 2: Edit View Controller’s XIB FileEdit ImageSwitcherViewController.xib in Interface BuilderChange the view’s background to blackAdd two image views and one page controllerone image view directly on top of the otherthe page controller should be “on top” of the two image views; it should be configured to have 5 pagestag the image views as “0” and “1”Use the outline view of IB’s window to select the views13Tuesday, March 8, 2011© Kenneth M. Anderson, 2011Step 3: Add Outlets in .h fileWe need to add outlets and proper ties for the controls, plus add two variables that will help us manage the views14Tuesday, March 8, 2011© Kenneth M. Anderson, 2011Step 4: Wire up the controlsBack in Interface Builderconnect the outlets to the various controlsImageView “one” should connect to the view with tag “0”ImageView “two” should connect to the view with tag “1”Save your work and head back to XCode15Tuesday, March 8, 2011© Kenneth M. Anderson, 2011Step 5: Write the CodeDon’t forget to synthesize your properties!We then need to write code for two methodsThe first is viewDidLoad; This is a view controller method that gets invoked just after it has created its view and just before that view gets displayedThis is your opportunity to initialize the viewThe second is pageTurning:; this is a method we will create ourselves; we’ll tell the page control to call this method when the user asks it to turn the page16Tuesday, March 8, 2011© Kenneth M. Anderson, 2011viewDidLoad (I)In this method, we willask one image view to load an imageeach image loaded will be cached automaticallyhide the other image viewset up our variables “front” and “back”tell the page control which method to invoke17Tuesday, March 8, 2011© Kenneth M. Anderson, 2011viewDidLoad (II)18Tuesday, March 8, 2011© Kenneth M. Anderson, 2011viewDidLoad (III)19Note the call (addTarget:action:forControlEvents:)that links up the page control with our method called pageTurning:We used an Objective-C function to create a reference to the method@selector(<methodname>)creates a reference to the specified method signature that can then be resolved at run time@selector(pageTurning:)ensures that the page control can call the correct methodTuesday, March 8, 2011© Kenneth M. Anderson, 2011Step 6: Implement switching images20We will make sure we can change the
View Full Document