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
480
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
390
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
160
Functional Programming in Swift
chriseidhof
40
19k
Lighter View Controllers
chriseidhof
4
210
Parsing with Blocks
chriseidhof
2
240
Other Decks in Programming
See All in Programming
高速開発のためのコード整理術
sutetotanuki
1
430
24時間止められないシステムを守る-医療ITにおけるランサムウェア対策の実際
koukimiura
2
170
kintone + ローカルLLM = ?
akit37
0
110
NOT A HOTEL - 建築や人と融合し、自由を創り出すソフトウェア
not_a_hokuts
2
380
ふん…おもしれぇ Parser。RubyKaigi 行ってやるぜ
aki_pin0
0
110
nilとは何か 〜interfaceの構造とnil!=nilから理解する〜 / Understanding nil in Go Interface Representation and Why nil != nil
kuro_kurorrr
0
230
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
190
Railsの気持ちを考えながらコントローラとビューを整頓する/tidying-rails-controllers-and-views-as-rails-think
moro
4
250
Sekiban + Microsoft Orleans のアクターをAWS対応しました / Sekiban + Microsoft Orleans actors are now supported on AWS.
tomohisa
0
120
izumin5210のプロポーザルのネタ探し #tskaigi_msup
izumin5210
1
330
ご飯食べながらエージェントが開発できる。そう、Agentic Engineeringならね。
yokomachi
1
210
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
250
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.3k
Producing Creativity
orderedlist
PRO
348
40k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
120
Building an army of robots
kneath
306
46k
Done Done
chrislema
186
16k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
72
Exploring anti-patterns in Rails
aemeredith
2
270
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
1.9k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
290
The browser strikes back
jonoalderson
0
710
Visualization
eitanlees
150
17k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.4k
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