Announcements Lab 2 is due tonight Lab 3 is posted Due next Wednesday Sept 30 th 1 Extensible - CSE 436 Software Networking Engineering Platform Workshop 1 Today s Topics Designing iphone Applica;ons Model- View- Controller (Why and How?) View Controllers 2 Extensible - CSE 436 Software Networking Engineering Platform Workshop 2
Designing iphone Applica;ons 3 Extensible - CSE 436 Software Networking Engineering Platform Workshop 3 Two Flavors of Mail 4 Extensible - CSE 436 Software Networking Engineering Platform Workshop 4
Organizing Content 5 Extensible - CSE 436 Software Networking Engineering Platform Workshop 5 Organinzing Content Focus on your user s data One thing at a ;me Screenfuls of content 6 Extensible - CSE 436 Software Networking Engineering Platform Workshop 6
PaMerns for Organizing Content Naviga;on Bar Tab Bar 7 Extensible - CSE 436 Software Networking Engineering Platform Workshop 7 Naviga;on Bar Hierarchy of content Drill down into greater detail 8 Extensible - CSE 436 Software Networking Engineering Platform Workshop 8
Tab Bar Self- contained modes 9 Extensible - CSE 436 Software Networking Engineering Platform Workshop 9 A Screenful of Content Slice of your applica;on Views, data, logic 10 Extensible - CSE 436 Software Networking Engineering Platform Workshop 10
Parts of a Screenful 11 Extensible - CSE 436 Software Networking Engineering Platform Workshop 11 Parts of a Screenful 12 Extensible - CSE 436 Software Networking Engineering Platform Workshop 12
Model- View- Controller (Why and How?) 13 Extensible - CSE 436 Software Networking Engineering Platform Workshop 13 Why Model- View- Controller? Ever used the word spagher to describe code? Clear responsibili;es make things easier to maintain Avoid having one monster class that does everything 14 Extensible - CSE 436 Software Networking Engineering Platform Workshop 14
Why Model- View- Controller? Separa;ng responsibili;es also leads to reusability By minimizing dependencies, you can take a model or view class you ve already wrimen and use it elsewhere Think of ways to write fewer lines of code 15 Extensible - CSE 436 Software Networking Engineering Platform Workshop 15 Communica;on and MVC KVO, notifications KVO, notifications target-action, delegation 16 Extensible - CSE 436 Software Networking Engineering Platform Workshop 16
View Controllers 17 Extensible - CSE 436 Software Networking Engineering Platform Workshop 17 Problem: Managing a Screenful Controller manages views, data and applica;on logic Apps are made up of many of these Would be nice to have a well- defined star;ng point A la UIView for views Common language for talking about controllers 18 Extensible - CSE 436 Software Networking Engineering Platform Workshop 18
Problem: Building Typical Apps Some applica;on flows are very common NavigaFon- based Tab bar- based Combine the two Don t reinvent the wheel Plug individual screens together to build an app 19 Extensible - CSE 436 Software Networking Engineering Platform Workshop 19 UIViewController Basic building block Manages a screenful of content Subclass to add your applica;on logic View Controller 20 Extensible - CSE 436 Software Networking Engineering Platform Workshop 20
Your and Apple View Controllers Create your own UIViewController subclass for each screenful Plug them together using exis;ng composite view controllers 21 Extensible - CSE 436 Software Networking Engineering Platform Workshop 21 Your and Our View Controllers Create your own UIViewController subclass for each screenful Plug them together using exis;ng composite view controllers 22 Extensible - CSE 436 Software Networking Engineering Platform Workshop 22
#import <UIKit/UIKit.h> Your View Controller Subclass @interface MyViewController : UIViewController { // A view controller will usually // manage views and data NSMutableArray *mydata; UILabel *mylabel; } //Expose some of its contents to clients @property NSArray *mydata; // And respond to ac;ons - (IBAc;on) dosomeac;on:(id)sender; 23 Extensible - CSE 436 Software Networking Engineering Platform Workshop 23 The View in View Controller UIViewController superclass has a view property @property (retain) UIView *view; Loads lazily On demand when requested OS figures this out Can be purged on demand as well (low memory) Sizing and posi;oning the view? Depends on where it s being used Don t make assumpfons, be flexible 24 Extensible - CSE 436 Software Networking Engineering Platform Workshop 24
Don t do it! When to call - loadview? Cocoa tends to embrace a lazy philosophy Call - release instead of - dealloc Call - setneedsdisplay instead of - drawrect: Allows work to be deferred Performance! Consider Fme to launching an applicafon 25 Extensible - CSE 436 Software Networking Engineering Platform Workshop 25 Crea;ng Your View in Code Override - loadview Never call this directly Create your views Set the view property Create view controller with - init // Subclass of UIViewController - (void)loadview { MyView *myview = [[MyView alloc] initwithframe:frame]; self.view = myview; // The view controller now owns the view [myview release]; // With ARC we no longer need this } 26 Extensible - CSE 436 Software Networking Engineering Platform Workshop 26
Crea;ng Your View with Storyboard Lay out a view in Storyboard File s owner is view controller class 27 Extensible - CSE 436 Software Networking Engineering Platform Workshop 27 Crea;ng Your View with Storyboard Lay out a view Storyboard File s owner is view controller class Hook up view outlet Create view controller with - initwithnibname:bundle: 28 Extensible - CSE 436 Software Networking Engineering Platform Workshop 28
View Controller Lifecycle - (id)initwithnibname:(nsstring *)nibname bundle:(nsbundle *)bundle { if (self == [super init...]) { // Perform ini;al setup, nothing view- related mydata = [[NSMutableArray alloc] init]; self.;tle = @ Foo ; } return self; } 29 Extensible - CSE 436 Software Networking Engineering Platform Workshop 29 View Controller Lifecycle - (void)viewdidload { // Your view has been loaded // Customize it here if needed view.someweirdproperty = YES; } 30 Extensible - CSE 436 Software Networking Engineering Platform Workshop 30
View Controller Lifecycle - (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; // Your view is about to show on the screen [self beginloadingdatafromtheweb]; [self startshowingloadingprogress]; } 31 Extensible - CSE 436 Software Networking Engineering Platform Workshop 31 View Controller Lifecycle - (void)viewwilldisappear:(bool)animated { [super viewwilldisappear:animated]; // Your view is about to leave the screen [self rememberscrollposi;on]; [self savedatatodisk]; } 32 Extensible - CSE 436 Software Networking Engineering Platform Workshop 32
Naviga;on View Controller Demo 33 Extensible - CSE 436 Software Networking Engineering Platform Workshop 33 34 Extensible - CSE 436 Software Networking Engineering Platform Workshop 34
Loading and Saving Data Lots of op;ons out there, depends on what you need NSUserDefaults Property lists SQLite Web services Covering in greater depth in a few weeks 35 Extensible - CSE 436 Software Networking Engineering Platform Workshop 35 Saving State Across App Launches NSUserDefaults to read and write prefs & state Singleton object: + (NSUserDefaults *)standarduserdefaults; Methods for storing & fetching common types: - (NSInteger)integerForKey:(NSString *)key; - (void)setinteger:(int)value forkey:(nsstring *)key; - (id)objectforkey:(nsstring *)key; - (void)setobject:(int)value forkey:(nsstring *)key; Find an appropriate ;me to store and restore your state 36 Extensible - CSE 436 Software Networking Engineering Platform Workshop 36
More View Controller Hooks Automa;cally rota;ng your user interface Low memory warnings 37 Extensible - CSE 436 Software Networking Engineering Platform Workshop 37 Suppor;ng Interface Rota;on - (BOOL)shouldAutorotateToInterfaceOrienta;on: (UIInterfaceOrienta;on)interfaceOrienta;on { } // This view controller only supports portraits return (interfaceorienta;on == UIInterfaceOrienta;onPortrait); 38 Extensible - CSE 436 Software Networking Engineering Platform Workshop 38
Suppor;ng Interface Rota;on - (BOOL)shouldAutorotateToInterfaceOrienta;on: (UIInterfaceOrienta;on)interfaceOrienta;on { } // This view controller supports all orienta;ons // except for upside- down. return (interfaceorienta;on!=uiinterfaceorienta;onportraitupsidedown); 39 Extensible - CSE 436 Software Networking Engineering Platform Workshop 39 Autoresizing Your Views view.autoresizingmask = UIViewAutoresizingFlexibleWidth UIViewAutoresizingFlexibleHeight; view.autoresizingmask = UIViewAutoresizingFlexibleWidth UIViewAutoresizingFlexibleTopMargin; 40 Extensible - CSE 436 Software Networking Engineering Platform Workshop 40
NSUserDefaults and Screen Orienta;on Demo 41 Extensible - CSE 436 Software Networking Engineering Platform Workshop 41 Lab 4 Demo 42 Extensible - CSE 436 Software Networking Engineering Platform Workshop 42
Final Projects 43 Extensible - CSE 436 Software Networking Engineering Platform Workshop 43