Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
NSSpain 2015
Soroush Khanlou
Overstuffed App Delegate
@interface SKTabBarController : UITabBarController
Model-View Binding
Model Mutation
Navigation FlowLayout
Subview Allocation
User Input
Data Transformation
Data Fetching
VI...
— Graham Lee
When you get overly attached to MVC, then
you look at every class you create and ask the
question “is this a ...
— Graham Lee
When you get overly attached to MVC, then
you look at every class you create and ask the
question “is this a ...
— Graham Lee
When you get overly attached to MVC, then
you look at every class you create and ask the
question “is this a ...
Model-View Binding
Model Mutation
Navigation FlowLayout
Subview Allocation
User Input
Data Transformation
Data Fetching
VI...
Controller
User Input
Model
Domain State
View
Display and Layout
Adapter
Mediates Model-View Interaction
Model
Domain State and Behavior
View
Display and Layout
Mutate
Update View
Notify
...
View Controller
Mediates Model-View Interaction
Model
Domain State and Behavior
View
Display and Layout
Mutate
Update View...
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
id object = [self.dataSource...
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
id object = [self.dataSource...
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
id object = [self.dataSource...
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
id object = [self.dataSource...
PhotoSelectionViewController
StraighteningViewController
FilteringViewController
CaptionViewController
Frameworks
vs
Libraries
Frameworks call you
vs
Libraries
Frameworks call you
You call
vs
Libraries
UIView
UIViewController
UIView
UIViewController
CALayer
UIView
UIViewController
CALayer
???
UIView
UIViewController
CALayer
Coordinators
App Coordinator
App Coordinator
aka Application Controller
What does a coordinator do?
Model-View Binding
Model Mutation
Navigation FlowLayout
Subview Allocation
User Input
Data Transformation
Data Fetching
VI...
Model-View Binding
Model Mutation
Navigation FlowLayout
Subview Allocation
User Input
Data Transformation
Data Fetching
VI...
Model-View Binding
Model Mutation
Navigation Flow
Layout
Subview Allocation
User Input
Data Transformation
Data Fetching
V...
Model-View Binding
Model Mutation
Navigation Flow
Layout
Subview Allocation
User Input
Data Transformation
Data Fetching
V...
Model-View Binding
Model Mutation
Navigation Flow
Layout
Subview Allocation
User Input
Data Transformation
Data Fetching
V...
Model-View Binding
Model Mutation
Navigation Flow
Layout
Subview Allocation
User Input
Data Transformation
Data Fetching
V...
Model-View Binding
Model Mutation
Navigation Flow
Layout
Subview Allocation
User Input
Data Transformation
Data Fetching
V...
How do you make a coordinator?
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)options {
self.window = [UI...
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)options {
self.window = [UI...
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)options {
self.window = [UI...
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)options {
self.window = [UI...
@interface AppCoordinator : NSObject
- (instancetype)initWithNavigationController:
(UINavigationController *)navigationController {
self = [super init];
if (!s...
- (void)start {
if ([self isLoggedIn]) {
[self showContent];
} else {
[self showAuthentication];
}
}
- (void)showAuthentication {
AuthCoordinator *authCoordinator =
[[AuthCoordinator alloc]
initWithNavigationController:
sel...
- (void)coordinatorDidAuth:(AuthCoordinator *)coordinator {
[self.childCoordinators removeObject:coordinator];
[self showC...
@implementation AuthCoordinator
- (instancetype)initWithNavigationController:
(UINavigationController *)navigationControll...
- (void)start {
FirstRunViewController *firstRun =
[FirstRunViewController new];
firstRun.delegate = self;
[self.navigatio...
- (void)firstRunViewControllerDidTapSignup:
(FirstRunViewController *)firstRun {
SignUpViewController *signup =
[[SignUpVi...
- (void)signUpViewController:(SignUpViewController *)signup
didTapSignupWithEmail:(NSString *)email
password:(NSString *)p...
Why are coordinators great?
1. View controllers are isolated
A B C D
A B C D
Coordinator
1. View controllers are isolated
2. View controllers are reusable
1. View controllers are isolated
2. View controllers are reusable
3. Every task is encapsulated
1. View controllers are isolated
2. View controllers are reusable
3. Every task is encapsulated
4. Display-binding is sepa...
1. View controllers are isolated
2. View controllers are reusable
3. Every task is encapsulated
4. Display-binding is sepa...
App
Coordinator
Auth
Coordinator
Content
Coordinator
Create Profile
Coordinator
Choose Image
Coordinator
Create Message
Co...
App
Coordinator
Auth
Coordinator
Content
Coordinator
Create Profile
Coordinator
Choose Image
Coordinator
Create Message
Co...
App
Coordinator
Auth
Coordinator
Content
Coordinator
Create Profile
Coordinator
Choose Image
Coordinator
Create Message
Co...
Thanks!
@khanlou
Coordinators
Coordinators
Upcoming SlideShare
Loading in …5
×

Coordinators

3,492 views

Published on

Use coordinators to organize your app's structure.

Published in: Software
  • Interesting. One question. When user presses BACK in a navigation bar, the current view controller is pop automatically. Shouldn't we UINavigationControllerDelegate or just a notification to inform about that to the coordinator and the the coordinator sends the pop message to the navigation controller? As far as I understand, with coordinators that responsibility belongs to the coordinator, not the vc or navigation controller. Thanks.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Coordinators

  1. 1. NSSpain 2015 Soroush Khanlou
  2. 2. Overstuffed App Delegate
  3. 3. @interface SKTabBarController : UITabBarController
  4. 4. Model-View Binding Model Mutation Navigation FlowLayout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER
  5. 5. — Graham Lee When you get overly attached to MVC, then you look at every class you create and ask the question “is this a model, a view, or a controller?”. Because this question makes no sense, the answer doesn’t either: anything that isn’t evidently data or evidently graphics gets put into the amorphous “controller” collection, which eventually sucks your entire codebase into its innards like a black hole collapsing under its own weight.
  6. 6. — Graham Lee When you get overly attached to MVC, then you look at every class you create and ask the question “is this a model, a view, or a controller?”. Because this question makes no sense, the answer doesn’t either: anything that isn’t evidently data or evidently graphics gets put into the amorphous “controller” collection, which eventually sucks your entire codebase into its innards like a black hole collapsing under its own weight.
  7. 7. — Graham Lee When you get overly attached to MVC, then you look at every class you create and ask the question “is this a model, a view, or a controller?”. Because this question makes no sense, the answer doesn’t either: anything that isn’t evidently data or evidently graphics gets put into the amorphous “controller” collection, which eventually sucks your entire codebase into its innards like a black hole collapsing under its own weight.
  8. 8. Model-View Binding Model Mutation Navigation FlowLayout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER
  9. 9. Controller User Input Model Domain State View Display and Layout
  10. 10. Adapter Mediates Model-View Interaction Model Domain State and Behavior View Display and Layout Mutate Update View Notify User Action
  11. 11. View Controller Mediates Model-View Interaction Model Domain State and Behavior View Display and Layout Mutate Update View Notify User Action
  12. 12. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { id object = [self.dataSource objectAtIndexPath:indexPath]; DetailViewController *detail = [[DetailViewController alloc] initWithObject:object]; [self.navigationController pushViewController:detail animated:YES]; }
  13. 13. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { id object = [self.dataSource objectAtIndexPath:indexPath]; DetailViewController *detail = [[DetailViewController alloc] initWithObject:object]; [self.navigationController pushViewController:detail animated:YES]; }
  14. 14. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { id object = [self.dataSource objectAtIndexPath:indexPath]; DetailViewController *detail = [[DetailViewController alloc] initWithObject:object]; [self.navigationController pushViewController:detail animated:YES]; }
  15. 15. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { id object = [self.dataSource objectAtIndexPath:indexPath]; DetailViewController *detail = [[DetailViewController alloc] initWithObject:object]; [self.navigationController pushViewController:detail animated:YES]; }
  16. 16. PhotoSelectionViewController StraighteningViewController FilteringViewController CaptionViewController
  17. 17. Frameworks vs Libraries
  18. 18. Frameworks call you vs Libraries
  19. 19. Frameworks call you You call vs Libraries
  20. 20. UIView UIViewController
  21. 21. UIView UIViewController CALayer
  22. 22. UIView UIViewController CALayer ???
  23. 23. UIView UIViewController CALayer Coordinators
  24. 24. App Coordinator
  25. 25. App Coordinator aka Application Controller
  26. 26. What does a coordinator do?
  27. 27. Model-View Binding Model Mutation Navigation FlowLayout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  28. 28. Model-View Binding Model Mutation Navigation FlowLayout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  29. 29. Model-View Binding Model Mutation Navigation Flow Layout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  30. 30. Model-View Binding Model Mutation Navigation Flow Layout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  31. 31. Model-View Binding Model Mutation Navigation Flow Layout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  32. 32. Model-View Binding Model Mutation Navigation Flow Layout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  33. 33. Model-View Binding Model Mutation Navigation Flow Layout Subview Allocation User Input Data Transformation Data Fetching VIEW CONTROLLER COORDINATOR VIEW MODEL
  34. 34. How do you make a coordinator?
  35. 35. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options { self.window = [UIWindow new]; self.rootViewController = [UINavigationController new]; self.coordinator = [[AppCoordinator alloc] initWithNavigationController:self.rootViewController]; [self.appCoordinator start]; [self.window makeKeyAndVisible]; }
  36. 36. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options { self.window = [UIWindow new]; self.rootViewController = [UINavigationController new]; self.coordinator = [[AppCoordinator alloc] initWithNavigationController:self.rootViewController]; [self.appCoordinator start]; [self.window makeKeyAndVisible]; }
  37. 37. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options { self.window = [UIWindow new]; self.rootViewController = [UINavigationController new]; self.coordinator = [[AppCoordinator alloc] initWithNavigationController:self.rootViewController]; [self.appCoordinator start]; [self.window makeKeyAndVisible]; }
  38. 38. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options { self.window = [UIWindow new]; self.rootViewController = [UINavigationController new]; self.coordinator = [[AppCoordinator alloc] initWithNavigationController:self.rootViewController]; [self.appCoordinator start]; [self.window makeKeyAndVisible]; }
  39. 39. @interface AppCoordinator : NSObject
  40. 40. - (instancetype)initWithNavigationController: (UINavigationController *)navigationController { self = [super init]; if (!self) return nil; _navigationController = navigationController; return self; }
  41. 41. - (void)start { if ([self isLoggedIn]) { [self showContent]; } else { [self showAuthentication]; } }
  42. 42. - (void)showAuthentication { AuthCoordinator *authCoordinator = [[AuthCoordinator alloc] initWithNavigationController: self.navigationController]; authCoordinator.delegate = self; [authCoordinator start]; [self.childCoordinators addObject:authCoordinator]; }
  43. 43. - (void)coordinatorDidAuth:(AuthCoordinator *)coordinator { [self.childCoordinators removeObject:coordinator]; [self showContent]; }
  44. 44. @implementation AuthCoordinator - (instancetype)initWithNavigationController: (UINavigationController *)navigationController { self = [super init]; if (!self) return nil; _navigationController = navigationController; return self; }
  45. 45. - (void)start { FirstRunViewController *firstRun = [FirstRunViewController new]; firstRun.delegate = self; [self.navigationController pushViewController:firstRun animated:NO]; }
  46. 46. - (void)firstRunViewControllerDidTapSignup: (FirstRunViewController *)firstRun { SignUpViewController *signup = [[SignUpViewController alloc] init]; signup.delegate = self; [self.navigationController pushViewController:signup animated:YES]; }
  47. 47. - (void)signUpViewController:(SignUpViewController *)signup didTapSignupWithEmail:(NSString *)email password:(NSString *)password { //... } Etc.
  48. 48. Why are coordinators great?
  49. 49. 1. View controllers are isolated
  50. 50. A B C D
  51. 51. A B C D Coordinator
  52. 52. 1. View controllers are isolated 2. View controllers are reusable
  53. 53. 1. View controllers are isolated 2. View controllers are reusable 3. Every task is encapsulated
  54. 54. 1. View controllers are isolated 2. View controllers are reusable 3. Every task is encapsulated 4. Display-binding is separate from side effects
  55. 55. 1. View controllers are isolated 2. View controllers are reusable 3. Every task is encapsulated 4. Display-binding is separate from side effects 5. Coordinators are fully in your control
  56. 56. App Coordinator Auth Coordinator Content Coordinator Create Profile Coordinator Choose Image Coordinator Create Message Coordinator Edit Profile Coordinator Choose Image Coordinator Choose Image Coordinator
  57. 57. App Coordinator Auth Coordinator Content Coordinator Create Profile Coordinator Choose Image Coordinator Create Message Coordinator Edit Profile Coordinator Choose Image Coordinator Choose Image Coordinator
  58. 58. App Coordinator Auth Coordinator Content Coordinator Create Profile Coordinator Choose Image Coordinator Create Message Coordinator Edit Profile Coordinator Choose Image Coordinator Choose Image Coordinator
  59. 59. Thanks! @khanlou

×