iPhone Development: Multiple Views

11,581 views

Published on

  • Be the first to comment

iPhone Development: Multiple Views

  1. 1. iPhone  Development:     Mul1ple  Views   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  2. 2. UIWindow?   •  UIWindow:  the  path  to  the  user   •  If  you  want  something  to  be  visible  for  the   user,  you  must  use  the  UIWindow!   •  UIView  objects  are  arranged  in  UIWindow  in   hierarchy.   •  Parent  object  is  called  superview.   –  UIWindow   •  UIView     –  UIBuJon  
  3. 3. UIWindow  and  UIView   @interface MultiViewExampleAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; } ... [window addSubview: someView];
  4. 4. UIView   •  UIView  is  a  base  class  for  or  the  controls   –  UIView   •  UIControl   –  UIBuJon   •  UIView  may  be  also  a  content  view,  or  canvas.   •  UIView  holds  several  controls:   –  [someUIView addSubView: someButton]; •  So  basically  we  could  have  a  UIWindow,  that  has   one  UIView  that  contains  two  UIViews  which   contains  several  controls  
  5. 5. U1lity  Applica1on   •  Primarily  one  view   •  Second  view  for  seLngs   •  Example:  Stock  -­‐   applica1on  
  6. 6. Tab  Bar  Applica1on   •  Mul1view  app  that   displays  a  row  of   buJons  at  the  boJom   of  the  screen.   •  Tapping  one  of  the   buJons  causes  new   view  to  be  ac1ve.   •  Example:  Phone-­‐app  
  7. 7. Naviga1on-­‐Based  Applica1on   •  Hierarchical  Informa1on   to  the  user   •  Example:  Mail-­‐app  
  8. 8. Controlling  Different  Content  Views   •  To  control  different  content  views,  you  must   have  some  kind  of  view  controller   –  UIViewController  (U/lity  app)   –  UITabBarController  (Tab  Bar  app)   –  UINaviga1onController  (Naviga1on  app)  
  9. 9. Root  Controller   •  Root  Controller  is  responsible  for  switching   the  views   •  Root  Controller  usually  is   –  UITabBarController –  UINavigationController –  Subclass  of  UIViewController •  Root  Controller  holds  content  views  
  10. 10. Controller  and  Content  View   Content  View   Root  Controller   Content  View   Content  View  
  11. 11. Rela1onship   Gray's  view   Root's  view  
  12. 12. Rela1onship   White's  view   Root's  view  
  13. 13. Content  View   •  Content  View  holds  controls  (widgets)   •  Content  View  consists  of   1.  View  Controller  (!)   2.  The  Nib  file   3.  Op1onally  subclass  of  UIView  (if  you  need  to   change  the  view  in  code)  
  14. 14. Controller  and  Content  View   Content  View   View1Controller.h   View1Controller.m   View1.xib   Content  View   Root  Controller   View2Controller.h   View2Controller.m   View2.xib   Content  View   View3Controller.h   View3Controller.m   View3.xib  
  15. 15. Hierarchy   UIViewController  /   Interface  Builder   UITabController  /   UIViewController   UINaviga1onController   (.xib)   Content  View   View  Controller   UIView   UIBuJon   Root  Controller   View  Controller   UIView   UIBuJon   View  Controller   UIView   UIBuJon  
  16. 16. Once  again:  Root  Controller   // Promise we will import these classes later @class GrayViewController; @class WhiteViewController; @interface SwitchMyViewController : UIViewController { GrayViewController *grayViewController; WhiteViewController *whiteViewController; } @property (nonatomic, retain) GrayViewController *grayViewController; @property (nonatomic, retain) WhiteViewController *whiteViewController; // This method can change views. You can have for example a button // that changes the content view - (IBAction) switchViews:(id) sender; @end
  17. 17. Content  View's  Controller   #import <UIKit/UIKit.h> @interface WhiteViewController: UIViewController { } // Possible actions and outlets -(IBAction) grayButtonPressed:(id) sender; @end
  18. 18. UIViewController  -­‐>  View?   •  Each  View  Controller  has  a  View...   –  ..where  all  the  controls,  widgets,  are  stored   •  The  view  is  usually  implemented  in  Interface   builder   •  So  you  should  have   –  MyView1Controller.h –  MyView1Controller.m –  MyView1.xib
  19. 19. Crea1ng  Controllers  
  20. 20. Crea1ng  .nib  files  

×