iOS Selectors Blocks and Delegation
Upcoming SlideShare
Loading in...5
×
 

iOS Selectors Blocks and Delegation

on

  • 3,596 views

 

Statistics

Views

Total Views
3,596
Views on SlideShare
3,596
Embed Views
0

Actions

Likes
0
Downloads
22
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

iOS Selectors Blocks and Delegation iOS Selectors Blocks and Delegation Presentation Transcript

  • iOS:  Selectors,  Delega1on,  Blocks   Jussi  Pohjolainen  
  • About  callbacks  •  iOS  provides  several  ways  of  handling  call   backs   1.  Selectors   •  Func1on  pointer,  passing  method  as  on  argument.     2.  Delegate  protocols     •  Several  call  back  methods   3.  Blocks   •  Anonymous  func1ons  that  have  access  to  local   variables  
  • Selectors  •  Selectors  are  a  way  to  pass  a  method  around.  •  In  Java:  reflec1on  •  Common  use  in  controls  (target  –  ac1on  paMern)     [myButton addTarget:self action:@selector(myButtonWasPressed) forControlEvents:UIControlEventTouchUpInside]; - (void)myButtonWasPressed { // Do something about it }
  • Selectors  - (void) testSelectors{ SEL variable; variable = @selector(animate); [self methodWithSelectorAsArgument: variable];}- (void) methodWithSelectorAsArgument: (SEL) argument{ [self performSelector:argument];}- (void) animate{ ...}
  • Delegate  •  Using  protocols  (interfaces)  you  can  define  a   contract  that  the  implementor  fulfills  •  Making  call  backs:   –  [locationManager setDelegate: self)•  Now  the  self  object  must  conform  to  protocol   and  it’s  protocol  methods  are  called  (call  back)   –  @property id<CLLocationManagerDelegate> delegate
  • Blocks  •  Good  mechanism  for  call  back  methods   –  Replaces  some  of  the  delegate  protocol  methods  •  Block  is  a  chunk  of  code  that  can  be  executed  at   some  future  1me  •  Blocks  are  func1ons  that  are  objects,  they  can  be   passed  as  variables.  •  Code  and  Handling  task  are  related  to  each   other  •  In  Java  7:  anonymous  inner  classes.  In  Java  8:   lambda  expressions  
  • Java  Thread thread = new Thread(new Runnable() { public void run() { // The code }});
  • Declara1on  Form  •  The  declara1on  form  of  block  is   –  return_type (^block_name) (param_type, param_type, …)•  Example   –  int (^add) (int, int)•  ^ =  “I’m  a  block”  
  • Block  Defini1on  •  The  declara1on  form  of  block  is   –  ^return_type (param_type, param_type, …) { ... return return_type; };•  Example   –  ^int (int n1, int n2) { return n1+n2; }  
  • Declara1on  and  Defini1on  together  // Declaration of block variable addint (^add) (int, int);// Definition for the add block variableadd = ^int (int n1, int n2){ return n1+n2;};// Usageint number = add(2,2);  
  • Block  as  an  Argument  // The method takes a block as an argument// The block must return int and it must have// two integer arguments- (void) someMethod: (int (^) (int, int)) variable{ NSLog(@"%i", variable(2,2));}- (void) someOtherMethod{ // Declaration of block variable add int (^add) (int, int); // Definition for the add block variable add = ^int (int n1, int n2) { return n1+n2; }; // Calling the someMethod with an argument block [self someMethod: add];}
  • Block  as  an  Anonymous  Argument  - (void) someMethod: (int (^) (int, int)) variable{ NSLog(@"%i", variable(2,2));}- (void)someOtherMethod{ [self someMethod: ^int (int n1, int n2) { return n1+n2; } ];}
  • UIView  Anima1on  without  Blocks  - (void)removeAnimationView:(id)sender { [animatingView removeFromSuperview];}- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [UIView beginAnimations:@"Example" context:nil]; [UIView setAnimationDuration:5.0]; [UIView setAnimationDidStopSelector:@selector(removeAnimationView)]; [animatingView setAlpha:0]; [animatingView setCenter:CGPointMake(animatingView.center.x+50.0, animatingView.center.y+50.0)]; [UIView commitAnimations];}
  • Anima1on  with  Blocks  - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [UIView animateWithDuration:5.0 animations:^{ [animatingView setAlpha:0]; [animatingView setCenter:CGPointMake(animatingView.center.x+50.0, animatingView.center.y+50.0)]; } completion:^(BOOL finished) { [animatingView removeFromSuperview]; } ];}
  • Block  Advantages  •  Simplifies  code  •  Keeps  code  together  •  Apple  official  recommenda1on  (use  block   methods  if  possible)  
  • Recap  •  Selectors   –  Invoke  a  method  but  you  don’t  the  name  of  the   method  at  compile  1me  •  Delegates   –  Great  when  there  is  need  for  several  call  back   methods  •  Blocks   –  Only  need  for  one  anonymous  method  with   access  of  local  variables,  blocks  are  great