Slide 1

Slide 1 text

Good Intentions A Path to Building Better ViewControllers Jay Thrash @jaythrash ! 360iDev, Denver August 2014

Slide 2

Slide 2 text

We need to talk…

Slide 3

Slide 3 text

Model View Controller

Slide 4

Slide 4 text

Massive View Controller

Slide 5

Slide 5 text

View Controller Misused

Slide 6

Slide 6 text

View Controller Mistreated

Slide 7

Slide 7 text

Delegate (v) Delegate (n)

Slide 8

Slide 8 text

I need _______________________ a DataSource for this TableView ViewController A day in the Life

Slide 9

Slide 9 text

I need _______________________ data fetched from the server ViewController A day in the Life

Slide 10

Slide 10 text

I need _______________________ someone to validate the form fields ViewController A day in the Life

Slide 11

Slide 11 text

I need _______________________ a CLLocationManagerDelegate ViewController A day in the Life

Slide 12

Slide 12 text

I need _______________________ just one more thing. It’s wafer thin. ViewController A day in the Life

Slide 13

Slide 13 text

It’s too much

Slide 14

Slide 14 text

It’s too much We can do better

Slide 15

Slide 15 text

Separation of Concerns Single Responsibility Principal Apply Two Principals

Slide 16

Slide 16 text

Stores Techniques 3 Delegate(v) the DataSources Intentions

Slide 17

Slide 17 text

Stores Encapsulates access to external sources of data.

Slide 18

Slide 18 text

Stores Encapsulates access to external sources of data. Manage Shared Resources ➡ Data Loading & Caching ➡ System Resources ➡ Network ➡ Location ➡ Motion Services ➡ Camera

Slide 19

Slide 19 text

Stores #import @class MJTLocation; @interface MJTLocationStore : NSObject + (MJTLocationStore *)store; - (MJTLocation *)locationById:(NSString*)locationId; @end

Slide 20

Slide 20 text

Stores #import @class MJTLocation; @interface MJTLocationStore : NSObject + (MJTLocationStore *)store; - (MJTLocation *)locationById:(NSString*)locationId; @end loc = [[MJTLocationStore store] locationById:id];

Slide 21

Slide 21 text

Stores Candidates for Stores ! ➡Web Services ➡Databases ➡Filesystem ➡Location/Motion Services ➡Camera

Slide 22

Slide 22 text

Delegate the DataSources

Slide 23

Slide 23 text

“Lighter View Controllers” obj.io Issue #1 ! ➡ Remove UITableViewDataSource responsibilities from ViewController ! ➡ Encapsulate into own class Delegate the DataSources

Slide 24

Slide 24 text

Delegate the DataSources -(id)itemAtIndexPath:(NSIndexPath*)indexPath {} -(NSInteger)numberOfSectionsInTableView:(UITableView *)tv -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section -(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath Typical DataSource Pattern

Slide 25

Slide 25 text

Delegate the DataSources -(id)itemAtIndexPath:(NSIndexPath*)indexPath {} ! -(NSInteger)numberOfSectionsInTableView:(UITableView *)tv ! -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section ! -(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath Typical DataSource Pattern

Slide 26

Slide 26 text

Delegate the DataSources DataSource

Slide 27

Slide 27 text

Backing Store ViewController Delegate the DataSources DataSource

Slide 28

Slide 28 text

Backing Store ViewController Delegate the DataSources DataSource

Slide 29

Slide 29 text

Backing Store ViewController Delegate the DataSources DataSource

Slide 30

Slide 30 text

Backing Store ViewController Delegate the DataSources DataSource @[x, y, z]

Slide 31

Slide 31 text

Backing Store ViewController Delegate the DataSources DataSource @[x, y, z] plist

Slide 32

Slide 32 text

Backing Store ViewController Delegate the DataSources DataSource @[x, y, z] plist CoreData

Slide 33

Slide 33 text

Backing Store ViewController Delegate the DataSources DataSource @[x, y, z] plist CoreData

Slide 34

Slide 34 text

Delegate the DataSources

Slide 35

Slide 35 text

Intentions

Slide 36

Slide 36 text

Intentions UIAlertViewDelegate UITextFieldDelegate UITextFieldDelegate IBOutlets/IBActions

Slide 37

Slide 37 text

Intentions Intention

Slide 38

Slide 38 text

Intentions

Slide 39

Slide 39 text

Intentions Michael ichael-May

Slide 40

Slide 40 text

Intentions @interface ViewController () @property (strong, nonatomic) IBOutlet UILabel *translatedNameLabel; @property (strong, nonatomic) IBOutlet UITextField *nameTextField; @property (strong, nonatomic) Person *person; @end ViewController

Slide 41

Slide 41 text

Intentions - (void)viewDidLoad { [super viewDidLoad]; self.person = [[Person alloc] init]; [self updateNameLabel]; } -(void)updateNameLabel { self.translatedNameLabel.text = self.person.name; } - (IBAction)convertTapped:(id)sender { self.person.name = self.nameTextField.text.pigLatinizedString; [self updateNameLabel]; } ViewController

Slide 42

Slide 42 text

Intentions - (void)viewDidLoad { [super viewDidLoad]; self.person = [[Person alloc] init]; [self updateNameLabel]; } -(void)updateNameLabel { self.translatedNameLabel.text = self.person.name; } - (IBAction)convertTapped:(id)sender { self.person.name = self.nameTextField.text.pigLatinizedString; [self updateNameLabel]; } ViewController PigLatinIntention

Slide 43

Slide 43 text

Intentions @interface ViewController () @property (strong, nonatomic) PersonContainer *personContainer; @end - (void)viewDidLoad { [super viewDidLoad]; Person *person = [[Person alloc] init]; self.personContainer.person = person; } ViewController.m

Slide 44

Slide 44 text

Intentions @interface PigLatinIntention () @property (weak, nonatomic) IBOutlet UITextField *textField; @property (weak, nonatomic) IBOutlet UILabel *translatedNameLabel; @property (weak, nonatomic) IBOutlet PersonContainer* personContainer; @end PigLatinIntention.m

Slide 45

Slide 45 text

Intentions @implementation PigLatinIntention - (IBAction)translate:(id)sender { self.personContainer.person.name = self.textField.text.pigLatinizedString; [self updateNameLabel]; } -(void)updateNameLabel { self.translatedNameLabel.text = self.personContainer.person.name; } @end PigLatinIntention.m

Slide 46

Slide 46 text

Intentions

Slide 47

Slide 47 text

Intentions

Slide 48

Slide 48 text

Intentions

Slide 49

Slide 49 text

Intentions

Slide 50

Slide 50 text

Intentions

Slide 51

Slide 51 text

Stores Techniques 3 Delegate(v) the DataSources Intentions

Slide 52

Slide 52 text

Higher Learning Topics to Google ➡ objc.io - Lighter View Controllers

Slide 53

Slide 53 text

Higher Learning Topics to Google ➡ objc.io - Lighter View Controllers ➡ Bendyworks - SRP & iOS ➡ Chris Eidhof - Intentions

Slide 54

Slide 54 text

Thanks! @jaythrash [email protected] http://jaythrash.com/talks