Lighter View Controllers

Lighter View Controllers

Talk at Cocoaheads.nl

(Architecture image by Manu Cornet)

Ade0c334ecff1448bb96f5f733bf1f83?s=128

Chris Eidhof | @chriseidhof

February 18, 2014
Tweet

Transcript

  1. Lighter View Controllers Chris Eidhof CocoaHeads NL

  2. MVC Model View Controller

  3. MVC Model View Controller Massive View Controller

  4. None
  5. What's the largest file? find . -name "*.m" -exec wc

    -l "{}" \; | sort -n
  6. Steps Project: A complete OS in < 20K lines

  7. View Controllers are the least reusable part

  8. A view controller intermediates between model and view

  9. Model logic

  10. View logic

  11. View layout

  12. Web Service

  13. Too much already?

  14. Delegates

  15. IBActions ?

  16. Pragma marks ??

  17. None
  18. Child View Controllers

  19. Table View Controllers ☆ Editing Mode ☆ Keyboard notifications ☆

    Flashing scroll indicators ☆ Clearing Selection ☆ Pull to Refresh
  20. None
  21. None
  22. View Controller Transitions

  23. None
  24. Pulling out protocols

  25. For example: UITableViewDataSource

  26. Creating a separate data source object @interface FetchedResultsControllerDataSource : NSObject

    <UITableViewDataSource, NSFetchedResultsControllerDelegate>
  27. Creating a separate data source object - (NSInteger)numberOfSectionsInTableView:(UITableView*)t { return

    self.fetchedResultsController.sections.count; }
  28. Creating a separate data source object - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)sectionIndex {

    id<NSFetchedResultsSectionInfo> section; section = self.frc.sections[sectionIndex]; return section.numberOfObjects; }
  29. Creating a separate data source object Working with the delegate

    - (UITableViewCell*)tableView:(UITableView*)tv cellForRowAtIndexPath:(NSIndexPath*)ip { id object = [self objectAtIndexPath:indexPath]; id cell = [tv dequeueReusableCellWithIdentifier:ruI forIndexPath:ip]; [self.delegate configureCell:cell withObject:object]; return cell; }
  30. Advantages of a separate data source ☆ Lighter view controller

    ☆ Testable ☆ Reusable And you can do this for other protocols, too...
  31. A separate data source ☆ NSArrayDataSource ☆ NSFRCollectionViewController ☆ ...

  32. Categories

  33. Configuring a cell - (UITableViewCell*)tableView:(UITableView*)tv cellForRowAtIndexPath:(NSIndexPath*)ip { PhotoCell *cell =

    [tableView dequeueReusableCellWithIdentifier:@"PhotoCell"]; Photo *photo = [self itemAtIndexPath:indexPath]; cell.photoTitleLabel.text = photo.name; NSString* date = [self.dateFormatter stringFromDate:photo.creationDate]; cell.photoDateLabel.text = date; }
  34. Configuring a cell Pulling out the reusable part @implementation PhotoCell

    (ConfigureForPhoto) - (void)configureForPhoto:(Photo *)photo { self.photoTitleLabel.text = photo.name; NSString* date = [self.dateFormatter stringFromDate:photo.creationDate]; self.photoDateLabel.text = date; } @end
  35. Configuring a cell The refactored delegate method - (UITableViewCell *)tableView:(UITableView

    *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:PhotoCellIdentifier]; [cell configureForPhoto:[self itemAtIndexPath:indexPath]]; return cell; }
  36. Using Interface Builder

  37. None
  38. Intentions

  39. Intentions View Controllers may only contain code that effects view

    changes in response to data changes. Source: http://bendyworks.com/geekville/articles/ 2014/2/single-responsibility-principle-ios
  40. Intentions The IBAction macro must not be used in a

    View Controller
  41. Intentions The @interface block in a View Controller's header file

    must be blank.
  42. Intentions A View Controller may not implement any extra *Delegate

    or *DataSource protocols except for observing Model changes.
  43. Intentions A View Controller may only do work in viewDidLoad

    (or awakeFromNib), viewDidAppear, viewDidDisappear, and in response to an observed change in the model layer.
  44. None
  45. Testing

  46. Testing View controllers are notoriously hard to test → Make

    them light.
  47. Commercial Break

  48. None
  49. None
  50. Questions? @chriseidhof