Slide 1

Slide 1 text

Applica'on,  MVC,  and  Views  

Slide 2

Slide 2 text

UIApplica'on   •  Every  applica'on  has  a  single  instance  of   UIApplica'on   •  Manages  the  status  bar  and  applica'on  icon  badge   •  Can  open  a  URL,  schedule  a  local  no'fica'on,  register   for  remote  no'fica'ons   •  Manages  all  open  windows  (UIWindow)   UIApplication *app = [UIApplication sharedApplication];  

Slide 3

Slide 3 text

Applica'on  Delegate   •  An  app  created  by  XCode  provides  a   UIApplica'onDelegate  class.   •  It  is  through  the  applica'on  delegate  that  you   watch  for  UIApplica'on  events.   •  application:didFinishLaunchingWithOptions •  applicationDidBecomeActive •  applicationWillEnterForeground •  applicationWillResignActive •  applicationDidEnterBackground •  applicationWillTerminate

Slide 4

Slide 4 text

MVC   Model   View   Controller   •  Presenta'on  layer   •  What  the  user  sees  and   interacts  with   •  App  data  (e.g.  Core  Data)   •  Business  logic   •  Intermediary  between   model  and  view   •  Receives  and  acts  on   control  events  

Slide 5

Slide 5 text

MVC  (Example  Scenario)   Model   View   Controller   •  Present  login  form  (labels,   buNons,  etc.)   •  Tell  controller  if  user  submiNed   the  form   •  Stores  user  data   •  Provides  interfaces  for  gePng   user  data   •  Decides  what  type  of  view  to   show   •  Waits  for  form  submission,   and  uses  model  to  validate   login  data  

Slide 6

Slide 6 text

Controls  and  Events   View   Controller   IBOutlet   IBAc'on   A  controller’s  reference  to  a  UIView  (e.g.  UIBuNon)   A  controller  method,  called  by  UIView  for  its  specifically  aNached  event     (e.g.  buNon  click)  

Slide 7

Slide 7 text

Controls  and  Events   ANaching  to  events  through  code:   - (void)viewDidLoad! {! [super viewDidLoad];! ! [self.submitButton addTarget:self action:@selector(buttonTapped:) ! forControlEvents:UIControlEventTouchUpInside];! }! ! - (IBAction)buttonTapped:(id)sender ! {! NSLog(@"Our button was tapped!");! }  

Slide 8

Slide 8 text

Views  (UIView)   •  Heirarchical:  has  a  single  superview  and   subviews   •  All  live  inside  UIWindow.  UIWindow  is  also  a   UIView.   •  Superviews  retain  their  subviews  

Slide 9

Slide 9 text

View  Structures   •  CGPoint:  loca'on  {  x,  y  }   •  CGSize:  dimension  {  width,  height  }   •  CGRect:  loca'on  and  dimension  {  origin,  size  }   CGPoint point = CGPointMake(50, 100);   CGSize size = "(150, 150);   CGRect rect = CGRectMake(50, 100, 150, 150);  

Slide 10

Slide 10 text

UIView.frame  and  UIView.bounds   •  Frame  and  Bounds  are  of  CGRect  type   •  Frame  is  rela've  to  the  superview's   coordinate  system   •  Bounds  is  rela've  to  the  local  coordinate   system  

Slide 11

Slide 11 text

UIView  Coordinate  System   (0,0) 320 +x +y 480 (70,50) 100 90 A   B   View  A  frame   View  B  frame   View  B  bounds   origin:  0,  0   size:  320,  480   origin:  70,  50   size:  100,  90   origin:  0,  0   size:  100,  90  

Slide 12

Slide 12 text

Programma'cally  crea'ng  views   CGRect rect = CGRectMake(10, 10, 200, 50);! UILabel *label = [[UILabel alloc] initWithFrame:rect];! label.text = @"Ouya";! ! [self.view addSubview:label];! [label release];  

Slide 13

Slide 13 text

UIViewController  Lifecycle   -­‐init   -­‐viewDidLoad   -­‐viewWillAppear   -­‐viewWillDisappear   -­‐viewDidAppear   -­‐viewWillUnload   -­‐viewDidUnload   -­‐viewDidDisappear   -­‐dealloc  

Slide 14

Slide 14 text

Exercise   (70,50) 100 90 A   B   é   ê   ç   è   Given  the  illustra'on  on  the  lef,   create  an  applica'on  that  moves  a   view  (B)  depending  on  the  pressed   buNon.   Rules:   •  The  View  B  should  be  created   programma'cally.  No  Interface   Builder!