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
460
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
380
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.3k
Functional Swift
chriseidhof
1
150
Functional Programming in Swift
chriseidhof
40
19k
Lighter View Controllers
chriseidhof
4
200
Parsing with Blocks
chriseidhof
2
230
Other Decks in Programming
See All in Programming
kiroでゲームを作ってみた
iriikeita
0
140
Google I/O Extended Incheon 2025 ~ What's new in Android development tools
pluu
1
220
Claude Code派?Gemini CLI派? みんなで比較LT会!_20250716
junholee
1
800
MCPで実現できる、Webサービス利用体験について
syumai
7
2.4k
Workers を定期実行する方法は一つじゃない
rokuosan
0
140
Scale out your Claude Code ~自社専用Agentで10xする開発プロセス~
yukukotani
2
460
Flutterと Vibe Coding で個人開発!
hyshu
1
220
Git Sync を超える!OSS で実現する CDK Pull 型デプロイ / Deploying CDK with PipeCD in Pull-style
tkikuc
4
520
GUI操作LLMの最新動向: UI-TARSと関連論文紹介
kfujikawa
0
350
Vibe Codingの幻想を超えて-生成AIを現場で使えるようにするまでの泥臭い話.ai
fumiyakume
21
10k
それ CLI フレームワークがなくてもできるよ / Building CLI Tools Without Frameworks
orgachem
PRO
17
3.6k
あまり知られていない MCP 仕様たち / MCP specifications that aren’t widely known
ktr_0731
0
220
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
134
9.5k
Optimizing for Happiness
mojombo
379
70k
The Straight Up "How To Draw Better" Workshop
denniskardys
235
140k
Unsuck your backbone
ammeep
671
58k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.8k
Fireside Chat
paigeccino
38
3.6k
4 Signs Your Business is Dying
shpigford
184
22k
GitHub's CSS Performance
jonrohan
1031
460k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
540
Java REST API Framework Comparison - PWX 2021
mraible
32
8.8k
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