Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Good Intentions: A Path to Better View Controllers

Jay Thrash
August 25, 2014

Good Intentions: A Path to Better View Controllers

Originally presented at 360iDev in Denver, CO on August 25, 2014

When building iOS apps within the traditional MVC framework, View Controllers unfortunately bear the brunt of being the de facto dumping ground for all manner of disparate functionality. As a result, they are often the biggest and most convoluted files within our iOS projects.

But it doesn’t have to be that way. In this session, we will learn how to unburden our View Controllers by applying the Single Responsibility Principle and delegation through functional Intentions. We’ll find that by adopting the principles of “Lighter” View Controllers our code becomes much more robust to change, yet remains easy to maintain and test.

Jay Thrash

August 25, 2014
Tweet

More Decks by Jay Thrash

Other Decks in Technology

Transcript

  1. Stores Encapsulates access to external sources of data. Manage Shared

    Resources ➡ Data Loading & Caching ➡ System Resources ➡ Network ➡ Location ➡ Motion Services ➡ Camera
  2. Stores #import <Foundation/Foundation.h> @class MJTLocation; @interface MJTLocationStore : NSObject +

    (MJTLocationStore *)store; - (MJTLocation *)locationById:(NSString*)locationId; @end
  3. Stores #import <Foundation/Foundation.h> @class MJTLocation; @interface MJTLocationStore : NSObject +

    (MJTLocationStore *)store; - (MJTLocation *)locationById:(NSString*)locationId; @end loc = [[MJTLocationStore store] locationById:id];
  4. “Lighter View Controllers” obj.io Issue #1 ! ➡ Remove UITableViewDataSource

    responsibilities from ViewController ! ➡ Encapsulate into own class Delegate the DataSources
  5. 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
  6. Intentions @interface ViewController ()<UITextFieldDelegate> @property (strong, nonatomic) IBOutlet UILabel *translatedNameLabel;

    @property (strong, nonatomic) IBOutlet UITextField *nameTextField; @property (strong, nonatomic) Person *person; @end ViewController
  7. 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
  8. 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
  9. Intentions @interface ViewController ()<UITextFieldDelegate> @property (strong, nonatomic) PersonContainer *personContainer; @end

    - (void)viewDidLoad { [super viewDidLoad]; Person *person = [[Person alloc] init]; self.personContainer.person = person; } ViewController.m
  10. Intentions @interface PigLatinIntention () @property (weak, nonatomic) IBOutlet UITextField *textField;

    @property (weak, nonatomic) IBOutlet UILabel *translatedNameLabel; @property (weak, nonatomic) IBOutlet PersonContainer* personContainer; @end PigLatinIntention.m
  11. 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
  12. Higher Learning Topics to Google ➡ objc.io - Lighter View

    Controllers ➡ Bendyworks - SRP & iOS ➡ Chris Eidhof - Intentions