UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015
Transcript of UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015
![Page 1: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/1.jpg)
Andrei Raifura iOS Department Manager, YOPESO
#CodeWăy
UIViewController: Fighting the monster
![Page 2: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/2.jpg)
Overview
• The “monster” problem
• A practical solution
• View Controllers from historical perspective
![Page 3: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/3.jpg)
Historical approach
![Page 4: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/4.jpg)
Historical approach
![Page 5: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/5.jpg)
Historical approach
![Page 6: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/6.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/7.jpg)
Conceptual MVC
Model View
Controller
UpdateUpdate
User ActionNotify
![Page 8: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/8.jpg)
ViewController’s MVC
Model ViewControllerView
Update
Notify
![Page 9: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/9.jpg)
Meet UIViewController
![Page 10: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/10.jpg)
The problem
![Page 11: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/11.jpg)
Things changed
![Page 12: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/12.jpg)
ViewController is the same (almost)
Model ViewControllerView
Update
Notify
![Page 13: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/13.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/14.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/15.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/16.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/17.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/18.jpg)
View layout
![Page 19: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/19.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/20.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/21.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/22.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/23.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/24.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/25.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/26.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/27.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/28.jpg)
iOS Architecture
![Page 29: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/29.jpg)
Looking into a View Controller
![Page 30: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/30.jpg)
Looking for a solution
![Page 31: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/31.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/32.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/33.jpg)
Demo
![Page 34: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/34.jpg)
Reduced Responsibilities
• View layout
• View management code
• View setup
![Page 35: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/35.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/36.jpg)
ViewController’s MVC
Model ViewControllerView
Update
Notify
![Page 37: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/37.jpg)
ViewController’s MVC
ViewControllerView
![Page 38: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/38.jpg)
ViewController’s MVC
ViewController ViewUpdate
User Action
![Page 39: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/39.jpg)
Demo
![Page 40: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/40.jpg)
• 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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/41.jpg)
• 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](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/42.jpg)
• Cannot use storyboards
• May lead to class explosion
• Requires some glue code
Disadvantages
![Page 43: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/43.jpg)
One more thing?
![Page 44: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/44.jpg)
Summary
• The “monster” problem
• A practical solution
• View Controllers from historical perspective
![Page 45: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/45.jpg)
Questions?
![Page 46: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/46.jpg)
Contacts
Raifura Andrei iOS Department Manager, YOPESO
#CodeWăy
![Page 47: UIViewController: Fighting the monster | Andrei Raifura | CodeWay 2015](https://reader031.fdocuments.net/reader031/viewer/2022030210/58a426541a28abec1a8b6d3b/html5/thumbnails/47.jpg)