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
480
5
Share
Lighter View Controllers
Talk at Cocoaheads.nl
(Architecture image by Manu Cornet)
Chris Eidhof | @chriseidhof
February 18, 2014
More Decks by Chris Eidhof | @chriseidhof
See All by Chris Eidhof | @chriseidhof
Dutch FP Day 2015
chriseidhof
2
400
Tiny Networking in Swift
chriseidhof
2
19k
Functional Swift - Brooklyn
chriseidhof
3
1.3k
Functional Swift - SF
chriseidhof
6
26k
Functional Swift
chriseidhof
6
1.3k
Functional Swift
chriseidhof
1
170
Functional Programming in Swift
chriseidhof
40
19k
Lighter View Controllers
chriseidhof
4
210
Parsing with Blocks
chriseidhof
2
250
Other Decks in Programming
See All in Programming
Spec-Driven Development with AI Agents (Workshop, May 2026)
antonarhipov
3
340
運転動画を検索可能にする〜Cosmos-Embed1とDatabricks Vector Searchで〜/cosmos-embed1-databricks-vector-search
studio_graph
1
680
Are We Really Coding 10× Faster with AI?
kohzas
0
150
過去のレビュー知見をSkillsで資産化した話
pkshadeck
PRO
1
1.8k
サークル参加から学ぶ、小さな事業の回し方
yuzneri
0
160
ソースコード→AST→オペコード、の旅を覗いてみる
o0h
PRO
1
130
いつか誰かが、と思っていた フロントエンド刷新5年間の実践知
kiichisugihara
1
260
PHPでバイナリをパースして理解するASN.1
muno92
PRO
0
430
クラウドネイティブなエンジニアに向ける Raycastの魅力と実際の活用事例
nealle
2
250
PicoRuby for IoT: Connecting to the Cloud with MQTT
yuuu
2
770
when storing skills in S3 file
watany
3
1.5k
Structured Concurrency, Scoped Values and Joiners in the JDK 25 26 27
josepaumard
1
150
Featured
See All Featured
Chasing Engaging Ingredients in Design
codingconduct
0
190
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.5k
Faster Mobile Websites
deanohume
310
31k
Embracing the Ebb and Flow
colly
88
5k
Agile that works and the tools we love
rasmusluckow
331
21k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
The Curse of the Amulet
leimatthew05
1
12k
How to train your dragon (web standard)
notwaldorf
97
6.6k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.6k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
170
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
55k
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