Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Lighter View Controllers
Search
Chris Eidhof | @chriseidhof
February 18, 2014
Programming
5
450
Lighter View Controllers
Talk at Cocoaheads.nl
(Architecture image by Manu Cornet)
Chris Eidhof | @chriseidhof
February 18, 2014
Tweet
Share
More Decks by Chris Eidhof | @chriseidhof
See All by Chris Eidhof | @chriseidhof
Dutch FP Day 2015
chriseidhof
2
370
Tiny Networking in Swift
chriseidhof
2
19k
Functional Swift - Brooklyn
chriseidhof
3
1.2k
Functional Swift - SF
chriseidhof
6
26k
Functional Swift
chriseidhof
6
1.2k
Functional Swift
chriseidhof
1
140
Functional Programming in Swift
chriseidhof
40
19k
Lighter View Controllers
chriseidhof
4
190
Parsing with Blocks
chriseidhof
2
220
Other Decks in Programming
See All in Programming
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
120
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
200
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
EMになってからチームの成果を最大化するために取り組んだこと/ Maximize team performance as EM
nashiusagi
0
100
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
940
subpath importsで始めるモック生活
10tera
0
320
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
340
Macとオーディオ再生 2024/11/02
yusukeito
0
370
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
340
初めてDefinitelyTypedにPRを出した話
syumai
0
420
React への依存を最小にするフロントエンド設計
takonda
3
530
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
427
64k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Designing for humans not robots
tammielis
250
25k
GraphQLとの向き合い方2022年版
quramy
43
13k
Site-Speed That Sticks
csswizardry
0
29
Producing Creativity
orderedlist
PRO
341
39k
Optimizing for Happiness
mojombo
376
70k
Designing the Hi-DPI Web
ddemaree
280
34k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Gamification - CAS2011
davidbonilla
80
5k
Transcript
Lighter View Controllers Chris Eidhof CocoaHeads NL
MVC Model View Controller
MVC Model View Controller Massive View Controller
None
What's the largest file? find . -name "*.m" -exec wc
-l "{}" \; | sort -n
Steps Project: A complete OS in < 20K lines
View Controllers are the least reusable part
A view controller intermediates between model and view
Model logic
View logic
View layout
Web Service
Too much already?
Delegates
IBActions ?
Pragma marks ??
None
Child View Controllers
Table View Controllers ☆ Editing Mode ☆ Keyboard notifications ☆
Flashing scroll indicators ☆ Clearing Selection ☆ Pull to Refresh
None
None
View Controller Transitions
None
Pulling out protocols
For example: UITableViewDataSource
Creating a separate data source object @interface FetchedResultsControllerDataSource : NSObject
<UITableViewDataSource, NSFetchedResultsControllerDelegate>
Creating a separate data source object - (NSInteger)numberOfSectionsInTableView:(UITableView*)t { return
self.fetchedResultsController.sections.count; }
Creating a separate data source object - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)sectionIndex {
id<NSFetchedResultsSectionInfo> section; section = self.frc.sections[sectionIndex]; return section.numberOfObjects; }
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; }
Advantages of a separate data source ☆ Lighter view controller
☆ Testable ☆ Reusable And you can do this for other protocols, too...
A separate data source ☆ NSArrayDataSource ☆ NSFRCollectionViewController ☆ ...
Categories
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; }
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
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; }
Using Interface Builder
None
Intentions
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
Intentions The IBAction macro must not be used in a
View Controller
Intentions The @interface block in a View Controller's header file
must be blank.
Intentions A View Controller may not implement any extra *Delegate
or *DataSource protocols except for observing Model changes.
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.
None
Testing
Testing View controllers are notoriously hard to test → Make
them light.
Commercial Break
None
None
Questions? @chriseidhof