Coordinators

NSSpain 2015
Soroush Khanlou
Coordinators
Overstuffed App Delegate
@interface SKTabBarController : UITabBarController
Model-View Binding
Model Mutation
Navigation FlowLayout
Subview Allocation
User Input
Data Transformation
Data Fetching
VIEW CONTROLLER
— 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.
— 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.
— 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.
Model-View Binding
Model Mutation
Navigation FlowLayout
Subview Allocation
User Input
Data Transformation
Data Fetching
VIEW CONTROLLER
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
User Action
View Controller
Mediates Model-View Interaction
Model
Domain State and Behavior
View
Display and Layout
Mutate
Update View
Notify
User Action
- (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];
}
- (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];
}
- (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];
}
- (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];
}
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
VIEW CONTROLLER
COORDINATOR
VIEW MODEL
Model-View Binding
Model Mutation
Navigation FlowLayout
Subview Allocation
User Input
Data Transformation
Data Fetching
VIEW CONTROLLER
COORDINATOR
VIEW MODEL
Model-View Binding
Model Mutation
Navigation Flow
Layout
Subview Allocation
User Input
Data Transformation
Data Fetching
VIEW CONTROLLER
COORDINATOR
VIEW MODEL
Model-View Binding
Model Mutation
Navigation Flow
Layout
Subview Allocation
User Input
Data Transformation
Data Fetching
VIEW CONTROLLER
COORDINATOR
VIEW MODEL
Model-View Binding
Model Mutation
Navigation Flow
Layout
Subview Allocation
User Input
Data Transformation
Data Fetching
VIEW CONTROLLER
COORDINATOR
VIEW MODEL
Model-View Binding
Model Mutation
Navigation Flow
Layout
Subview Allocation
User Input
Data Transformation
Data Fetching
VIEW CONTROLLER
COORDINATOR
VIEW MODEL
Model-View Binding
Model Mutation
Navigation Flow
Layout
Subview Allocation
User Input
Data Transformation
Data Fetching
VIEW CONTROLLER
COORDINATOR
VIEW MODEL
How do you make a coordinator?
- (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];
}
- (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];
}
- (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];
}
- (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];
}
@interface AppCoordinator : NSObject
- (instancetype)initWithNavigationController:
(UINavigationController *)navigationController {
self = [super init];
if (!self) return nil;
_navigationController = navigationController;
return self;
}
- (void)start {
if ([self isLoggedIn]) {
[self showContent];
} else {
[self showAuthentication];
}
}
- (void)showAuthentication {
AuthCoordinator *authCoordinator =
[[AuthCoordinator alloc]
initWithNavigationController:
self.navigationController];
authCoordinator.delegate = self;
[authCoordinator start];
[self.childCoordinators addObject:authCoordinator];
}
- (void)coordinatorDidAuth:(AuthCoordinator *)coordinator {
[self.childCoordinators removeObject:coordinator];
[self showContent];
}
@implementation AuthCoordinator
- (instancetype)initWithNavigationController:
(UINavigationController *)navigationController {
self = [super init];
if (!self) return nil;
_navigationController = navigationController;
return self;
}
- (void)start {
FirstRunViewController *firstRun =
[FirstRunViewController new];
firstRun.delegate = self;
[self.navigationController
pushViewController:firstRun
animated:NO];
}
- (void)firstRunViewControllerDidTapSignup:
(FirstRunViewController *)firstRun {
SignUpViewController *signup =
[[SignUpViewController alloc] init];
signup.delegate = self;
[self.navigationController
pushViewController:signup
animated:YES];
}
- (void)signUpViewController:(SignUpViewController *)signup
didTapSignupWithEmail:(NSString *)email
password:(NSString *)password {
//...
}
Etc.
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 separate from side effects
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
Coordinators
App
Coordinator
Auth
Coordinator
Content
Coordinator
Create Profile
Coordinator
Choose Image
Coordinator
Create Message
Coordinator
Edit Profile
Coordinator
Choose Image
Coordinator
Choose Image
Coordinator
App
Coordinator
Auth
Coordinator
Content
Coordinator
Create Profile
Coordinator
Choose Image
Coordinator
Create Message
Coordinator
Edit Profile
Coordinator
Choose Image
Coordinator
Choose Image
Coordinator
App
Coordinator
Auth
Coordinator
Content
Coordinator
Create Profile
Coordinator
Choose Image
Coordinator
Create Message
Coordinator
Edit Profile
Coordinator
Choose Image
Coordinator
Choose Image
Coordinator
Thanks!
@khanlou
1 of 61

More Related Content

Featured(20)

ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
Alireza Esmikhani30.2K views
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking6.9K views
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
Erica Santiago25.1K views
9 Tips for a Work-free Vacation9 Tips for a Work-free Vacation
9 Tips for a Work-free Vacation
Weekdone.com7.1K views
I Rock Therefore I Am. 20 Legendary Quotes from PrinceI Rock Therefore I Am. 20 Legendary Quotes from Prince
I Rock Therefore I Am. 20 Legendary Quotes from Prince
Empowered Presentations142.8K views
How to Map Your FutureHow to Map Your Future
How to Map Your Future
SlideShop.com275.1K views
Read with Pride | LGBTQ+ ReadsRead with Pride | LGBTQ+ Reads
Read with Pride | LGBTQ+ Reads
Kayla Martin-Gant1.1K views

Coordinators