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

Lighter View Controllers

Lighter View Controllers

Slides of the talk I gave at NSSpain. Probably makes more sense with the video, or read the articles: http://www.objc.io/issue-1/

Chris Eidhof | @chriseidhof

September 19, 2013
Tweet

More Decks by Chris Eidhof | @chriseidhof

Other Decks in Technology

Transcript

  1. What’s the largest file in your project? 0 200 400

    600 800 Wednesday, September 18, 13
  2. find . -name "*.m" -exec wc -l "{}" \; |

    sort -n Wednesday, September 18, 13
  3. 154 ./ViewControllers/THUserPrioritiesViewController.m 154 ./ViewControllers/THZoomingNavigationController.m 155 ./Extensions/NSArray+Extensions.m 155 ./Extensions/UIView+Extensions.m 168 ./Controllers/THPriorityTimelineCollectionController.m

    179 ./Controllers/THUserScreenInstancesController.m 183 ./ViewControllers/THRootViewController.m 183 ./ViewControllers/THScreenInstanceViewController.m 185 ./Model/User+Extensions.m 192 ./ViewControllers/THEditPriorityViewController.m 221 ./ViewControllers/THScreeningBarViewController.m 243 ./Views/THTimeLineView.m 263 ./ViewControllers/THCardsViewController.m 279 ./Views/THSkillboxView.m 325 ./THStyleSheet.m Wednesday, September 18, 13
  4. 154 ./ViewControllers/THUserPrioritiesViewController.m 154 ./ViewControllers/THZoomingNavigationController.m 155 ./Extensions/NSArray+Extensions.m 155 ./Extensions/UIView+Extensions.m 168 ./Controllers/THPriorityTimelineCollectionController.m

    179 ./Controllers/THUserScreenInstancesController.m 183 ./ViewControllers/THRootViewController.m 183 ./ViewControllers/THScreenInstanceViewController.m 185 ./Model/User+Extensions.m 192 ./ViewControllers/THEditPriorityViewController.m 221 ./ViewControllers/THScreeningBarViewController.m 243 ./Views/THTimeLineView.m 263 ./ViewControllers/THCardsViewController.m 279 ./Views/THSkillboxView.m 325 ./THStyleSheet.m Wednesday, September 18, 13
  5. Some Stats 18 ./NestedTodoList/main.m 30 ./Item.m 31 ./NestedTodoList/Store.m 49 ./NestedTodoList/PersistentStack.m

    52 ./NestedTodoList/AppDelegate.m 255 ./NestedTodoList/ItemViewController.m Wednesday, September 18, 13
  6. What does it do? ‣ View Setup ‣ Table View

    Delegate ‣ Table View Datasource ‣ Fetched Results Controller Delegate ‣ Adding items ‣ Deleting Items ‣ ... Wednesday, September 18, 13
  7. Custom Datasource What does it do? ‣ View Setup ‣

    Table View Delegate ‣ Table View Datasource ‣ Fetched Results Controller Delegate ‣ Adding items ‣ Deleting Items ‣ ... Wednesday, September 18, 13
  8. Custom Datasource Model What does it do? ‣ View Setup

    ‣ Table View Delegate ‣ Table View Datasource ‣ Fetched Results Controller Delegate ‣ Adding items ‣ Deleting Items ‣ ... Wednesday, September 18, 13
  9. Model - (BOOL)textFieldShouldReturn:(UITextField*)t { NSString* title = t.text; NSUInteger order

    = self.parent.children.count; Item* item = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:self.moc]; item.title = title; item.parent = self.parent; item.order = @(order); ... } Wednesday, September 18, 13
  10. Model - (BOOL)textFieldShouldReturn:(UITextField*)t { NSString* title = t.text; [Item insertItemWithTitle:title

    parent:self.parent inManagedObjectContext:self.moc]; ... } Wednesday, September 18, 13
  11. - (void)deleteItem:(id)object { Item* item = object; NSSet* siblings =

    item.parent.children; NSPredicate* predicate = [NSPredicate pre NSSet* itemsAfterSelf = [siblings filtere [itemsAfterSelf enumerateObjectsUsingBloc { sibling.order = @(sibling.order.integ }]; [item.moc deleteObject:item]; } Wednesday, September 18, 13
  12. - (void)deleteItem:(id)object { Item* item = object; NSSet* siblings =

    item.parent.children; NSPredicate* predicate = [NSPredicate pre NSSet* itemsAfterSelf = [siblings filtere [itemsAfterSelf enumerateObjectsUsingBloc { sibling.order = @(sibling.order.integ }]; [item.moc deleteObject:item]; } Wednesday, September 18, 13
  13. - (void)prepareForDeletion { NSSet* siblings = self.parent.children; NSPredicate* p =

    [NSPredicate predicateWithFormat:@"order > %@", self.order]; NSSet* itemsAfterSelf = [siblings filteredSetUsingPredicate:p]; [itemsAfterSelf enumerateObjectsUsingBlock: ^(Item* sibling, BOOL* stop) { sibling.order = @(sibling.order.integerValue - 1); }]; } Wednesday, September 18, 13
  14. - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { id object = [self.frc objectAtIndexPath:indexPath]; id

    cell = [tableView dequeueReusableCellWithIdentifier:self.reuseIdentifier forIndexPath:indexPath]; [self.delegate configureCell:cell withObject:object]; return cell; } Wednesday, September 18, 13
  15. - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { id object = [self.frc objectAtIndexPath:indexPath]; id

    cell = [tableView dequeueReusableCellWithIdentifier:self.reuseIdentifier forIndexPath:indexPath]; [self.delegate configureCell:cell withObject:object]; return cell; } Wednesday, September 18, 13
  16. Some Stats (3) 25 ./NestedTodoList/Store.m 49 ./NestedTodoList/PersistentStack.m 52 ./NestedTodoList/AppDelegate.m 64

    ./Item.m 111 ./NestedTodoList/FRCDataSource.m 162 ./NestedTodoList/ItemViewController.m Wednesday, September 18, 13
  17. Superclass disadvantages You only have 1 superclass Harder to switch

    from table view to collection view Easier to make mistakes: e.g. forget to call super methods. Wednesday, September 18, 13
  18. Superclass disadvantages You only have 1 superclass Harder to switch

    from table view to collection view Easier to make mistakes: e.g. forget to call super methods. Wednesday, September 18, 13
  19. Superclass disadvantages You only have 1 superclass Harder to switch

    from table view to collection view Easier to make mistakes: e.g. forget to call super methods. objc_requires_super Wednesday, September 18, 13
  20. Superclass disadvantages You only have 1 superclass Harder to switch

    from table view to collection view Easier to make mistakes: e.g. forget to call super methods. objc_requires_super Wednesday, September 18, 13
  21. UITableViewController ‣ Clears selection ‣ Reloads data ‣ Flashes scroll

    indicators ‣ Editing ‣ Auto-scrolls to first responder ‣ Refresh control ‣ .... Wednesday, September 18, 13
  22. ... or in code [self addChildViewController:sliderController]; [self.view addSubview:sliderController]; [UIView animateWithDuration:0.25

    animations:^{ sliderController.view.alpha = 1; [sliderController didMoveToParentViewController:self]; }]; Wednesday, September 18, 13
  23. @interface MyCell : UITableViewCell @property (weak) IBOutlet UISlider *slider; @property

    (weak) IBOutlet UISwitch *switch; @end Wednesday, September 18, 13
  24. - (void)testNibLoading; { id mockNavController = [self autoVerifiedMockForClass: [UINavigationController class]];

    PhotosViewController *photosViewController = [[PhotosViewController alloc] init]; id photosViewControllerMock = [self autoVerifiedPartialMockForObject: photosViewController]; [[[photosViewControllerMock stub] andReturn:mockNavController] navigationController]; Wednesday, September 18, 13
  25. Tools Sort files by length Know your libraries Refactor: Use

    XCode 5 AppCode Wednesday, September 18, 13
  26. Tools Pull out data sources Use View Controller Containment Move

    code to models and views Wednesday, September 18, 13