47
Andrei Raifura iOS Department Manager, YOPESO #CodeWăy UIViewController: Fighting the monster

UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

  • Upload
    yopeso

  • View
    230

  • Download
    0

Embed Size (px)

Citation preview

Page 1: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Andrei Raifura iOS Department Manager, YOPESO

#CodeWăy

UIViewController: Fighting the monster

Page 2: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Overview

• The “monster” problem

• A practical solution

• View Controllers from historical perspective

Page 3: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Historical approach

Page 4: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Historical approach

Page 5: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Historical approach

Page 6: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Meet UIViewController

• Contains the View

• Manages the View

• Plays well with Interface Builder

• Defines a screen

• Conforms to MVC

Page 7: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Conceptual MVC

Model View

Controller

UpdateUpdate

User ActionNotify

Page 8: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s MVC

Model ViewControllerView

Update

Notify

Page 9: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Meet UIViewController

Page 10: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

The problem

Page 11: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Things changed

Page 12: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController is the same (almost)

Model ViewControllerView

Update

Notify

Page 13: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s Responsibilities

• View layout

• View management code

• Navigation logic

• View setup

• Core Data and NSFetchedResultsController code

• Delegate methods

• Other “decision making” code

Page 14: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

View setup

@interface FMViewController () @property (weak, nonatomic) IBOutlet UITextField *usernameTextField; @property (weak, nonatomic) IBOutlet UITextField *passwordTextField; @property (weak, nonatomic) IBOutlet UIView *feedbackView; @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

@end

@implementation FMViewController

Page 15: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

View setup

- (void)viewDidLoad { [super viewDidLoad]; self.myButton.titleLabel.font = myCustomFont; UIImage *stretchableImage = [self.myButton setBackgroundImage:stretchableImage forControlState:UIControlStateNormal]; }

Page 16: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s Responsibilities

• View layout

• View management code

• Navigation logic

• View setup

• Core Data and NSFetchedResultsController code

• Delegate methods

• Other “decision making” code

Page 17: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

View layout

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.myLabel.frame = /* ... */ self.myButton.frame = /* ... */ }

Page 18: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

View layout

Page 19: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s Responsibilities

• View layout

• View management code

• Navigation logic

• View setup

• Core Data and NSFetchedResultsController code

• Delegate methods

• Other “decision making” code

Page 20: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s Responsibilities

• View layout

• View management code

• Navigation logic

• View setup

• Core Data and NSFetchedResultsController code

• Delegate methods

• Other “decision making” code

Page 21: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Navigation logic

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { id item = [self itemAtIndexPath:indexPath]; DetailsViewController *detailsVC = [[DetailsViewController alloc] initWithItem:item]; [self.navigationController pushViewController:detailsVC animated:YES]; }

Page 22: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s Responsibilities

• View layout

• View management code

• Navigation logic

• View setup

• Core Data and NSFetchedResultsController code

• Delegate methods

• Other “decision making” code

Page 23: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Core Data code

• View layout

• View management code

• Navigation logic

• View setup

• Core Data and NSFetchedResultsController code

• Delegate methods

• Other “decision making” code

Page 24: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s Responsibilities

• View layout

• View management code

• Navigation logic

• View setup

• Core Data and NSFetchedResultsController code

• Delegate methods

• Other “decision making” code

Page 25: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s Responsibilities

• View layout

• View management code

• Navigation logic

• View setup

• Core Data and NSFetchedResultsController code

• Delegate methods

• Other “decision making” code

Page 26: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Decision making code

DetailsViewController *detailsVC = /* ... */; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { // display details in the split view controller's // details pane } else { // push details to the current navigation controller }

Page 27: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s Responsibilities

• View layout

• View management code

• Navigation logic

• View setup

• Core Data and NSFetchedResultsController code

• Delegate methods

• Other “decision making” code

Page 28: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

iOS Architecture

Page 29: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Looking into a View Controller

Page 30: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Looking for a solution

Page 31: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s Responsibilities

• View layout

• View management code

• Navigation logic

• View setup

• Core Data and NSFetchedResultsController code

• Delegate methods

• Other “decision making” code

Page 32: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s Responsibilities

• View layout

• View management code

• Navigation logic

• View setup

• Core Data and NSFetchedResultsController code

• Delegate methods

• Other “decision making” code

Page 33: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Demo

Page 34: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Reduced Responsibilities

• View layout

• View management code

• View setup

Page 35: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

View Controller issues

• Cannot be tested (using unit tests)

• Requires changes when the UI is changed

Page 36: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s MVC

Model ViewControllerView

Update

Notify

Page 37: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s MVC

ViewControllerView

Page 38: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

ViewController’s MVC

ViewController ViewUpdate

User Action

Page 39: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Demo

Page 40: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

• Use View Models to encapsulate the information the view displays

• Put the IBOutlets in the private interface

• Create classes for views

• Use XIBs to define your layout

• Use a delegate to communicate asynchronous events from the view

• View keeps the datasources

Separate View from View Controller

Page 41: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

• Testable View Controllers

• Ability to change the View without touching the View Controller

• More granular code

• It plays well with MVVM and VIPER concepts

Advantages

Page 42: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

• Cannot use storyboards

• May lead to class explosion

• Requires some glue code

Disadvantages

Page 43: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

One more thing?

Page 44: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Summary

• The “monster” problem

• A practical solution

• View Controllers from historical perspective

Page 45: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Questions?

Page 46: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015

Contacts

Raifura Andrei iOS Department Manager, YOPESO

[email protected]

[email protected]

#CodeWăy

Page 47: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015