iOS:	  Selectors,	  Delega1on,	  Blocks	                Jussi	  Pohjolainen	  
About	  callbacks	  •  iOS	  provides	  several	  ways	  of	  handling	  call	     backs	      1.  Selectors	         •   ...
Selectors	  •  Selectors	  are	  a	  way	  to	  pass	  a	  method	  around.	  •  In	  Java:	  reflec1on	  •  Common	  use	 ...
Selectors	  - (void) testSelectors{    SEL variable;    variable = @selector(animate);    [self methodWithSelectorAsArgume...
Delegate	  •  Using	  protocols	  (interfaces)	  you	  can	  define	  a	     contract	  that	  the	  implementor	  fulfills	...
Blocks	  •  Good	  mechanism	  for	  call	  back	  methods	      –  Replaces	  some	  of	  the	  delegate	  protocol	  met...
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,       par...
Block	  Defini1on	  •  The	  declara1on	  form	  of	  block	  is	         –  ^return_type (param_type, param_type, …)      ...
Declara1on	  and	  Defini1on	  together	  // Declaration of block variable addint (^add) (int, int);// Definition for the a...
Block	  as	  an	  Argument	  // The method takes a block as an argument// The block must return int and it must have// two...
Block	  as	  an	  Anonymous	  Argument	  - (void) someMethod: (int (^) (int, int)) variable{    NSLog(@"%i", variable(2,2)...
UIView	  Anima1on	  without	  Blocks	  - (void)removeAnimationView:(id)sender {    [animatingView removeFromSuperview];}- ...
Anima1on	  with	  Blocks	  - (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    [UIView animateWith...
Block	  Advantages	  •  Simplifies	  code	  •  Keeps	  code	  together	  •  Apple	  official	  recommenda1on	  (use	  block	 ...
Recap	  •  Selectors	      –  Invoke	  a	  method	  but	  you	  don’t	  the	  name	  of	  the	         method	  at	  compi...
Upcoming SlideShare
Loading in...5
×

iOS Selectors Blocks and Delegation

4,449

Published on

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

No Downloads
Views
Total Views
4,449
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
52
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

iOS Selectors Blocks and Delegation

  1. 1. iOS:  Selectors,  Delega1on,  Blocks   Jussi  Pohjolainen  
  2. 2. 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  
  3. 3. 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 }
  4. 4. Selectors  - (void) testSelectors{ SEL variable; variable = @selector(animate); [self methodWithSelectorAsArgument: variable];}- (void) methodWithSelectorAsArgument: (SEL) argument{ [self performSelector:argument];}- (void) animate{ ...}
  5. 5. 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
  6. 6. 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  
  7. 7. Java  Thread thread = new Thread(new Runnable() { public void run() { // The code }});
  8. 8. 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”  
  9. 9. 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; }  
  10. 10. 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);  
  11. 11. 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];}
  12. 12. 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; } ];}
  13. 13. 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];}
  14. 14. 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]; } ];}
  15. 15. Block  Advantages  •  Simplifies  code  •  Keeps  code  together  •  Apple  official  recommenda1on  (use  block   methods  if  possible)  
  16. 16. 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  
  1. A particular slide catching your eye?

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

×