cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt...

25
1 - CSE 436 – Software Engineering Workshop Announcements Lab 3 is due on Wednesday Sept 30th Lab 4 is posted – Due Oct 14 th 2 - CSE 436 – Software Engineering Workshop Topics NSUserDefaults and Screen Rota@on View Controllers Applica@on Data Flow Customizing Naviga@on Tab Bar Controllers Combining Approaches

Upload: others

Post on 26-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 1 1 - CSE 436 – Software Engineering Workshop

Announcements  

•  Lab  3  is  due  on  Wednesday  Sept  30th  

•  Lab  4  is  posted  – Due  Oct  14th  

Extensible Networking Platform 2 2 - CSE 436 – Software Engineering Workshop

Topics  

•  NSUserDefaults  and  Screen  Rota@on  

•  View  Controllers  

•  Applica@on  Data  Flow  

•  Customizing  Naviga@on  

•  Tab  Bar  Controllers  

•  Combining  Approaches  

Page 2: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 3 3 - CSE 436 – Software Engineering Workshop

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  

Extensible Networking Platform 4 4 - CSE 436 – Software Engineering Workshop

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  

Page 3: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 5 5 - CSE 436 – Software Engineering Workshop

More  View  Controller  Hooks  

•  Automa@cally  rota@ng  your  user  interface  

•  Low  memory  warnings  

Extensible Networking Platform 6 6 - CSE 436 – Software Engineering Workshop

Suppor@ng  Interface  Rota@on    -­‐(BOOL)shouldAutorotateToInterfaceOrienta@on:  

   (UIInterfaceOrienta@on)interfaceOrienta@on  {          //  This  view  controller  only  supports  portraits  

 return  (interfaceOrienta@on  ==  UIInterfaceOrienta@onPortrait);    }  

Page 4: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 7 7 - CSE 436 – Software Engineering Workshop

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);    }  

Extensible Networking Platform 8 8 - CSE 436 – Software Engineering Workshop

Autoresizing  Your  Views    view.autoresizingMask  =  UIViewAutoresizingFlexibleWidth  |                                                          UIViewAutoresizingFlexibleHeight;      view.autoresizingMask  =  UIViewAutoresizingFlexibleWidth  |                                                          UIViewAutoresizingFlexibleTopMargin;  

Page 5: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop

View  Controllers  

Extensible Networking Platform 10 10 - CSE 436 – Software Engineering Workshop

UIViewController  

•  Basic  building  block  •  Manages  a  screenful  of  content  •  Subclass  to  add  your  applica@on  logic  

View Controller

Page 6: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 11 11 - CSE 436 – Software Engineering Workshop

“Your”  and  Apple  View  Controllers  

•  Create  your  own  UIViewController  subclass  for  each  screenful  

•  Plug  them  together  using  exis@ng  composite  view  controllers  

Extensible Networking Platform 12 12 - CSE 436 – Software Engineering Workshop

“Your”  and  “Our”  View  Controllers  

•  Create  your  own  UIViewController  subclass  for  each  screenful  

•  Plug  them  together  using  exis@ng  composite  view  controllers  

Page 7: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 13 13 - CSE 436 – Software Engineering Workshop

Naviga@on  Controllers  

Extensible Networking Platform 14 14 - CSE 436 – Software Engineering Workshop

UINaviga@onController  

•  Stack  of  view  controllers  •  Naviga@on  bar  

Page 8: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 15 15 - CSE 436 – Software Engineering Workshop

How  It  Fits  Together  

•  Top  view  controller’s  view  

•  Top  view  controller’s  @tle  

•  Previous  view  controller’s  @tle  

Extensible Networking Platform 16 16 - CSE 436 – Software Engineering Workshop

Modifying  the  Naviga@on  Stack  

•  Push  to  add  a  view  controller  (iOS  8  and  later)    -­‐(void)showViewController:(UIViewController  *)viewController  

   sender:(id);    •  Push  to  add  a  view  controller  (iOS  7  and  earlier,  deprecated  in  iOS  8)  -­‐(void)pushViewController:(UIViewController  *)viewController  

   animated:(BOOL)animated;    

•  Pop  to  remove  a  view  controller        -­‐(UIViewController  *)popViewControllerAnimated:(BOOL)animated;  

Page 9: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 17 17 - CSE 436 – Software Engineering Workshop

Pushing  Your  First  View  Controller  

-­‐  (void)applica@onDidFinishLaunching  {  //  Create  a  naviga@on  controller  navController  =  [[UINaviga@onController  alloc]  init];      //  Push  the  first  view  controller  on  the  stack  [navController  pushViewController:firstViewController  animated:NO];    //  Add  the  naviga@on  controller’s  view  to  the  window  [window  addSubview:navController.view];  }  

Extensible Networking Platform 18 18 - CSE 436 – Software Engineering Workshop

In  Response  to  User  Ac@ons  •  Push  from  within  a  view  controller  on  the  stack  

-­‐  (void)someAc@on:(id)sender  {  //  Poten@ally  create  another  view  controller  UIViewController  *viewController  =  ...;    [self.naviga@onController  pushViewController:viewController  animated:YES];    }  

•  Almost  never  call  pop  directly!  –   AutomaAcally  invoked  by  the  back  buEon  

Page 10: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 19 19 - CSE 436 – Software Engineering Workshop

Applica@on  Data  Flow  

Extensible Networking Platform 20 20 - CSE 436 – Software Engineering Workshop

A  Controller  for  Each  Screen  

Page 11: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 21 21 - CSE 436 – Software Engineering Workshop

Extensible Networking Platform 22 22 - CSE 436 – Software Engineering Workshop

Connec@ng  View  Controllers  

•  Mul@ple  view  controllers  may  need  to  share  data  

•  One  may  need  to  know  about  what  another  is  doing  –  Watch  for  added,  removed  or  edited  data  –  Other  interesAng  events  

Page 12: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 23 23 - CSE 436 – Software Engineering Workshop

How  Not  To  Share  Data  

•  Global  variables  or  singletons  –  This  includes  your  applicaAon  delegate!  

•  Direct  dependencies  make  your  code  less  reusable  – And  more  difficult  to  debug  &  test  

Don’t do this!

Extensible Networking Platform 24 24 - CSE 436 – Software Engineering Workshop

Best  Prac@ces  for  Data  Flow  

•  Figure  out  exactly  what  needs  to  be  communicated  

•  Define  input  parameters  for  your  view  controller  

•  For  communica@ng  back  up  the  hierarchy,  use  loose  coupling  –  Define  a  generic  interface  for  observers  (like  delegaAon)  

Page 13: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 25 25 - CSE 436 – Software Engineering Workshop

           Customizing  Naviga@on  

Extensible Networking Platform 26 26 - CSE 436 – Software Engineering Workshop

Customizing  Naviga@on  

•  Bukons  or  custom  controls  •  Interact  with  the  en@re  screen  

Page 14: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 27 27 - CSE 436 – Software Engineering Workshop

UINaviga@onItem  

•  Describes  appearance  of  the  naviga@on  bar  –  Title  string  or  custom  Atle  view  –  LeP  &  right  bar  buEons  – More  properAes  defined  in  UINavigaAonBar.h  

•  Every  view  controller  has  a  naviga@on  item  for  customizing  – Displayed  when  view  controller  is  on  top  of  the  stack  

Extensible Networking Platform 28 28 - CSE 436 – Software Engineering Workshop

Naviga@on  Item  Ownership  

Page 15: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 29 29 - CSE 436 – Software Engineering Workshop

Displaying  a  Title  

•  UIViewController  already  has  a  @tle  property  – @property(nonatomic,copy)  NSString  *Atle;  

•  Naviga@on  item  inherits  automa@cally  –  Previous  view  controller’s  Atle  is  displayed  in  back  buEon  

   viewController.@tle  =  @“Detail”;  

Extensible Networking Platform 30 30 - CSE 436 – Software Engineering Workshop

Lem  &  Right  Bukons  

•  UIBarBukonItem  –  Special  object,  defines  appearance  &  behavior  for  items  in  navigaAon  bars  and  toolbars  

•  Display  a  string,  image  or  predefine    system  item  

•  Target  +  ac@on  (like  a  regular  bukon)  

Page 16: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 31 31 - CSE 436 – Software Engineering Workshop

Text  Bar  Bukon  Item  

-­‐  (void)viewDidLoad  {        UIBarBukonItem  *fooBukon  =  [[UIBarBukonItem  alloc]                  initWithTitle:@"Foo”                  style:UIBarBukonItemStyleBordered                  target:self                  ac@on:@selector(foo:)];              [email protected]  =  fooBukon;            [fooBukon  release];  }  

Extensible Networking Platform 32 32 - CSE 436 – Software Engineering Workshop

System  Bar  Bukon  Item  

 (void)viewDidLoad  {            UIBarBukonItem  *addBukon  =  [[UIBarBukonItem  alloc]            initWithBarBukonSystemItem:UIBarBukonSystemItemAdd            style:UIBarBukonItemStyleBordered            target:self            ac@on:@selector(add:)];            [email protected]  =  addBukon;            [addBukon  release];  }  

Page 17: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 33 33 - CSE 436 – Software Engineering Workshop

Naviga@on  Controller  with  Custom  Bukon  Demo  

Extensible Networking Platform 34 34 - CSE 436 – Software Engineering Workshop

Edit/Done  Bukon  

•  Very  common  pakern  •  Every  view  controller  has    one  available  –  Target/acAon  already  set  up  

•  Edit/Done  Bukon    [email protected]  =  self.editBukonItem;  

 //  Called  when  the  user  toggles  the  edit/done  bukon  -­‐  (void)setEdi@ng:(BOOL)edi@ng  animated:(BOOL)animated  {          //  Update  appearance  of  views  }  

Page 18: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 35 35 - CSE 436 – Software Engineering Workshop

Custom  Title  View  

•  Arbitrary  view  in  place  of  the  @tle    UISegmentedControl  *segmentedControl  =  ...  self.naviga@onItem.@tleView  =  segmentedControl;  [segmentedControl  release];  

Extensible Networking Platform 36 36 - CSE 436 – Software Engineering Workshop

Back  Bukon  

•  Some@mes  a  shorter  back  bukon  is  needed    Self.@tle  =  @”Hello  there,”;  UIBarBukonItem  *heyBukon  =  [[UIBarBukonItem  alloc]  initWithTitle:@“Hey!”  ...];  [email protected]  =  heyBukon;  [heyBukon  release];  

Page 19: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 37 37 - CSE 436 – Software Engineering Workshop

               Tab  Bar  Controllers  

Extensible Networking Platform 38 38 - CSE 436 – Software Engineering Workshop

UITabBarController  Array  of  view  controllers  •  •  Tab  bar  

Page 20: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 39 39 - CSE 436 – Software Engineering Workshop

How  it  Fits  Together  

•  Selected  view  controller’s  view  •  All  view  controller’s  @tles  

Extensible Networking Platform 40 40 - CSE 436 – Software Engineering Workshop

Seong  Up  a  Tab  Bar  Controller  

(void)applica@onDidFinishLaunching  //  Create  a  tab  bar  controller  tabBarController  =  [[UITabBarController  alloc]  init];      //  Set  the  array  of  view  controllers  tabBarController.viewControllers  =  [NSArray  arrayWithObjects:  

firstVC,  secondVC,  nil];    //  Add  the  tab  bar  controller’s  view  to  the  window  [window  addSubview:tabBarController.view];    }  

Page 21: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 41 41 - CSE 436 – Software Engineering Workshop

Tab  Bar  Appearance  

•  View  controllers  can  define  their  appearance  in  the  tab  bar  

•  UITabBarItem  –  Title  +  image  or  system  item    

•  Each  view  controller  comes  with  a  tab  bar  item  for  customizing  

Extensible Networking Platform 42 42 - CSE 436 – Software Engineering Workshop

Crea@ng  Tab  Bar  Items  

•  Title  and  image  

-­‐  (void)viewDidLoad  {          self.tabBarItem  =  [[UITabBarItem  alloc]          initWithTitle:@“Playlists”          image:[UIImage  imageNamed:@“music.png”]          tag:0]  }  

Page 22: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 43 43 - CSE 436 – Software Engineering Workshop

Crea@ng  Tab  Bar  Items  

•  System  item  

-­‐  (void)viewDidLoad  {          self.tabBarItem  =  [[UITabBarItem  alloc]          initWithTabBarSystemItem:      UITabBarSystemItemBookmarks  tag:0]  

}  

Extensible Networking Platform 44 44 - CSE 436 – Software Engineering Workshop

More  View  Controllers  

• What  happens  when  a  tab  bar  controller  has  too  many  view  controllers  to  display  at  once?  –  “More”  tab  bar  item  displayed  automaAcally  

– User  can  navigate  to  remaining  view  controllers  

Page 23: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 45 45 - CSE 436 – Software Engineering Workshop

Custom  Tab  Bar  Controller  Demo  

Extensible Networking Platform 46 46 - CSE 436 – Software Engineering Workshop

             Combining  Approaches  

Page 24: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 47 47 - CSE 436 – Software Engineering Workshop

Tab  Bar  +  Naviga@on  Controllers  

Extensible Networking Platform 48 48 - CSE 436 – Software Engineering Workshop

Tab  Bar  +  Naviga@on  Controllers  

Page 25: cse436 lecture7 9-23-15todd/cse436/cse436_lecture7_9-23-15.pdf · cse436_lecture7_9-23-15.ppt Author: Todd Sproull Created Date: 9/23/2015 2:19:48 PM

Extensible Networking Platform 49 49 - CSE 436 – Software Engineering Workshop

Nes@ng  Naviga@on  Controllers  •  Create  a  tab  bar  controller          tabBarController  =  [[UITabBarController  alloc]  init];  

•  Create  each  naviga@on  controller          navController  =  [[UINaviga@onController  alloc]  init];  

 [navController  pushViewController:firstViewController          animated:NO];  

 •  Add  them  to  the  tab  bar  controller          tabBarController.viewControllers  =  [NSArray  arrayWithObjects:                  navController,  

             anotherNavController,                    someViewController,                nil];  

Extensible Networking Platform 50 50 - CSE 436 – Software Engineering Workshop

Final  Project  Examples