Your SlideShare is downloading. ×
0
iOS:	  View	  Controllers	              Jussi	  Pohjolainen	  Tampere	  University	  of	  Applied	  Sciences	  
Screen,	  Window,	  UIView	  
View	  Controller	  
Several	  Screens?	  •  Typical	  iOS	  app	  has	  several	  screens	  •  Usually	  this	  is	  implemented	  so	  that	 ...
UIViewController	  #import <UIKit/UIKit.h>@interface MainScreenViewController :UIViewController@end
Adding	  a	  BuIon	  to	  the	  View	  of	                   ViewController	  - (void)viewDidLoad{    [super viewDidLoad];...
Root	  Controller	  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchO...
Root	  Controller?	  •  SeKng	  a	  view	  controller	  as	  root	  controller	  of	  a	     window	      –  Add’s	  view	...
XIB?	  •  It’s	  usually	  wise	  to	  implement	  the	  UI	  in	  xib	  •  You	  can	  create	  new	  empty	  xib	  file	 ...
View	  Controller	  and	  View	  •  The	  View	  Controller	  has	  a	  property	  View	  •  In	  XIB,	  you	  can	  add	 ...
Content	  View	  Controller	  vs	             Container	  View	  Controller	  •  Content	  View	  Controller	     –  Conte...
Tab	  Bar	  Controller	  
NavigaMon	  Controller	  
Complex	  App	  
UITABVIEWCONTROLLER	  
UITabController	  •  To	  change	  a	  view	  controller	  •  Holds	  array	  of	  view	  controllers	  •  Really	  easy	 ...
UITabController:	  Add	  Titles	  •  ViewController	  class	  has	  a	  property	     tabBarItem!	  
Init	  in	  more	  detail	  •  The	  init	  method	  here	  loads	  the	  xib-­‐file…	  you	     pass	  the	  xib-­‐file	  n...
Init	  in	  more	  detail	  •  When	  iniMalizing	  the	  controller 	  	      –  	  SettingsViewController* settingsview ...
UINAVIGATIONCONTROLLER	  
About	  Nav	  Controller	  •  Presents	  data	  organized	  hierarchically	  •  Stack-­‐based	  collecMon	  of	  view	  co...
CreaMng	  a	  UINavigaMonController	  // Create view controllerSettingsViewController* settingsview = [[SettingsViewContro...
Modifying	  NavigaMon	  Stack	  •  Add	  another	  view	  to	  stack	      –  [navController       pushViewController:othe...
Passing	  informaMon	  // Get text from UITextFieldNSString* myText = [[self someTextField] text];// Create the view contr...
Receiving	  informaMon	  @interface SettingsView1Controller : UIViewController{}// Attribute and set+get methods for text@...
The	  Nav	  Bar	  
The	  Nav	  Bar	  
UINavigaMonItem	  •  Set	  a	  Mtle	       –  UINavigationItem *n = [self navigationItem];     –  [n setTitle:@"Settings"]...
Right	  BuIon	  
iOS: View Controllers
iOS: View Controllers
Upcoming SlideShare
Loading in...5
×

iOS: View Controllers

1,525

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,525
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
50
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "iOS: View Controllers"

  1. 1. iOS:  View  Controllers   Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  
  2. 2. Screen,  Window,  UIView  
  3. 3. View  Controller  
  4. 4. Several  Screens?  •  Typical  iOS  app  has  several  screens  •  Usually  this  is  implemented  so  that  you  have   several  View  Controller  classes,  one  for  each   screen  •  UIViewController  class  holds  a  property  that   points  to  UIView  (which  is  visible  in  the   screen)  
  5. 5. UIViewController  #import <UIKit/UIKit.h>@interface MainScreenViewController :UIViewController@end
  6. 6. Adding  a  BuIon  to  the  View  of   ViewController  - (void)viewDidLoad{ [super viewDidLoad]; // Initialize a view, this could be a custom view also.. UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setTitle:@"Main1" forState: UIControlStateNormal]; // Add the view to the controller [self setView: button];}
  7. 7. Root  Controller  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. MainScreenViewController* mainscreen = [[MainScreenViewController alloc] init]; [[self window] setRootViewController:mainscreen]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;}
  8. 8. Root  Controller?  •  SeKng  a  view  controller  as  root  controller  of  a   window   –  Add’s  view  controller’s  view  to  window   –  AutomaMcally  resizes  the  view  to  be  the  same  size   than  window  
  9. 9. XIB?  •  It’s  usually  wise  to  implement  the  UI  in  xib  •  You  can  create  new  empty  xib  file  to  your   project  •  Set  the  File’s  owner  to  the  view  controller!  
  10. 10. View  Controller  and  View  •  The  View  Controller  has  a  property  View  •  In  XIB,  you  can  add  to  canvas  a  UIView  and   connect  it  as  outlet  of  the  view  controller’s   view!  
  11. 11. Content  View  Controller  vs   Container  View  Controller  •  Content  View  Controller   –  Content  on  the  screen  using  Views   –  Usually  has  a  .xib  •  Container  View  Controller   –  Content  owned  by  other  View  Controllers   –  Parent  to  other  controllers   –  Container  manages  a  API  to  manage  children  (change   screens)   –  For  example:  Tab  Bar  Controller,  Naviga<on   Controller  
  12. 12. Tab  Bar  Controller  
  13. 13. NavigaMon  Controller  
  14. 14. Complex  App  
  15. 15. UITABVIEWCONTROLLER  
  16. 16. UITabController  •  To  change  a  view  controller  •  Holds  array  of  view  controllers  •  Really  easy  to  implement    
  17. 17. UITabController:  Add  Titles  •  ViewController  class  has  a  property   tabBarItem!  
  18. 18. Init  in  more  detail  •  The  init  method  here  loads  the  xib-­‐file…  you   pass  the  xib-­‐file  name  to  the  method:  
  19. 19. Init  in  more  detail  •  When  iniMalizing  the  controller     –   SettingsViewController* settingsview = [[SettingsViewController alloc] init];•  This  is  equivalent  to  this!   –  SettingsViewController* settingsview = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];•  What  happens  if  nibname  is  nil?   –  Search  for  a  xib  file  whose  name  is  the  same  as  your   class  and  search  inside  of  this  app  bundle!
  20. 20. UINAVIGATIONCONTROLLER  
  21. 21. About  Nav  Controller  •  Presents  data  organized  hierarchically  •  Stack-­‐based  collecMon  of  view  controllers   –  Stack:  path  taken  by  the  user  through  the   hierarchical  data   –  BoIom  stack:  starMng  point,  top  stack:  current   posiMon  •  Holds   –  NavigaMon  bar,  a  back  buIon  and  opMonal  custom   views  
  22. 22. CreaMng  a  UINavigaMonController  // Create view controllerSettingsViewController* settingsview = [[SettingsViewControlleralloc] init];// Create navigation controller.// Initialize it with the first screenUINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:settingsview];// Add navigation controller to window [[self window] setRootViewController:navController];
  23. 23. Modifying  NavigaMon  Stack  •  Add  another  view  to  stack   –  [navController pushViewController:otherViewController animated:YES];•  To  get  pointer  to  navController  in   ViewController,  use   •  [[self navigationController] pushViewController:otherV iewController animated:YES];
  24. 24. Passing  informaMon  // Get text from UITextFieldNSString* myText = [[self someTextField] text];// Create the view controllerSettingsView1Controller* sv1 = [[SettingsView1Controller alloc] init];// Pass the text to the view controller[sv1 setText: myText];// Push the view controller to nav controller. viewDidLoad is// called![[self navigationController] pushViewController:sv1 animated:YES];
  25. 25. Receiving  informaMon  @interface SettingsView1Controller : UIViewController{}// Attribute and set+get methods for text@property NSString* text;@property IBOutlet UILabel* label;* * *- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view from its nib. // Set the given text to label: [[self label] setText: [self text]];}
  26. 26. The  Nav  Bar  
  27. 27. The  Nav  Bar  
  28. 28. UINavigaMonItem  •  Set  a  Mtle   –  UINavigationItem *n = [self navigationItem]; –  [n setTitle:@"Settings"];•  Other  properMes   –  MtleView  -­‐>  Can  have  any  view  on  navigaMon  bar   –  rightBarBuIonItem  -­‐>  another  buIon  to  the  right   –  See  documentaMon  
  29. 29. Right  BuIon  
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×