iOS - Working with block handler
If you’re not new to Objective-C, then I’m pretty sure you know about delegate. Normally we use it when the outerViewController
that want to trigger some actions when user touch a button on innerViewController
.
A typically example will be ListViewController
& DetailViewController
.
In delegate way:
ListViewController.m
1 | DetailViewController *controller = [[DetailViewController alloc] init]; |
DetailViewController.h
1 | ... |
So when user do anything with this DetailViewController
DetailViewController.m
1 | - (void)actionTouched:(UIButton *)sender |
let say touched a button, then invoke the delegate method, so that theListViewController
get notify from DetailViewController
.
Alternative: Using block
DetailViewController.h
1 | - (void)doneSomething:(void (^)(void))actionHandler; |
Create a method signature with an empty block handler.
DetailViewController.m
1 | @interface DetailViewController () |
When user touched on the button, then it invoke the done
block, the doneSomething:
is called by ListViewController
ListViewController.m
1 | DetailViewController *controller = [[DetailViewController alloc] init]; |
References: