SlideShare a Scribd company logo
1 of 21
Download to read offline
Intro to Obj-C 
Design Patterns 
Or how I learned to be less bad 
Haris Amin
GLIMPSE 
A fun way to meet 
new people through 
Instagram
Design Patterns 
• Why do we care? 
• Let’s explore two design patterns in 
Cocoa 
1. Notification Center 
2. Delegates
Notification Center 
• NSNotificationCenter class 
• Easiest to learn (IMHO) but also easiest to 
abuse 
• Very flexible
Notification Center 
• Let’s Grab a NotificationCenter ‘instance’ 
[NSNotificationCenter defaultCenter] 
! 
• Then PUBLISH a specific message (2 ways) 
1. With content 
2. Without Content
Publish Message Without Content 
• We want to notify that something specific 
‘happened’ 
• Method Signature 
- (void)postNotificationName:(NSString 
*)notificationName object:(id)notificationSender
Publish Message Without Content 
- (void)postNotificationName:(NSString 
*)notificationName object:(id)notificationSender 
• E.G. The Video’s current time just updated 
[[NSNotificationCenter defaultCenter] 
postNotificationName:@"TimeUpdated" object:self];
Publish Message With Content 
- (void)postNotificationName:(NSString 
*)notificationName object:(id)notificationSender 
userInfo:(NSDictionary *)userInfo 
• E.G. The Video’s current time just updated 
WITH the actual current time 
[[NSNotificationCenter defaultCenter] 
postNotificationName:@"TimeUpdated" object:self 
userInfo:@{@"currentTime": 20}];
Subscribing to a Message 
- (void)addObserver:(id)notificationObserver 
selector:(SEL)notificationSelector name:(NSString 
*)notificationName object:(id)notificationSender 
• In our VideoPlayer class we will just subscribe 
to the NotificationCenter Message 
[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(handleTimeUpdate:) 
name:@"TimeUpdated" 
object:nil]; 
- (void)handleTimeUpdate:(NSNotification *)note{ 
// note.userInfo is the CONTENT we passed 
// ... 
}
Notification Center… 
THE GOOD 
• Really flexible 
• Don’t have to define any sort of protocol for your 
messages 
• You can subscribe and publish messages in any 
number of places
Notification Center… 
THE BAD 
• TOO flexible?! 
• Don’t have to define any sort of protocol for your 
messages 
• You can subscribe and publish messages in any 
number of of places
Delegates 
• Very solid pattern 
• Vigorously multitude of Cocoa classes 
(UITableView, UICollectionView, UIAlterView.. 
etc.) 
• You have to define the protocol of the 
messages you send
A Personal Note… 
• I consumed delegates without really 
understanding how they work 
• I used to be scared of trying to implement 
one myself
TIME TO GAIN A 
SUPERPOWER!
Delegates 
• Very solid pattern 
• Vigorously multitude of Cocoa classes 
(UITableView, UICollectionView, UIAlterView.. 
etc.) 
• You have to define the protocol of the 
messages you send
Define a Delegate 
Protocol 
• Very solid pattern 
• Vigorously multitude of Cocoa classes 
(UITableView, UICollectionView, UIAlterView.. 
etc.) 
• You have to define the protocol of the 
messages you send
Define Protocol 
@protocol VideoDelegate; 
! 
@interface Video: NSObject 
! 
@property (nonatomic, weak) id<VideoDelegate> delegate; 
! 
@end 
! 
@protocol VideoDelegate <NSObject> 
@required 
//... 
! 
@optional 
-(void)video:(Video *)video updatedTime:(NSUInteger)currentTime; 
! 
@end // end of delegate protocol
Delegating Methods in 
the Protocol 
• In our Video class, where need be, we will 
delegate respective messages 
@implementation Video 
//.. 
! 
if (self.delegate && [self.delegate 
respondsToSelector:@selector(video:updatedTime)]) { 
[self.delegate video:updatedTime]; 
} 
//.. 
@end
Implement Protocol 
#include "Video.h" // needed to include the @delegate protocol 
info 
! 
@interface VideoPlayer : UIViewController <VideoDelegate> 
@end 
! 
@implementation VideoPlayer 
//.. 
! 
-(void)video:(Video *)video updatedTime:(NSUInteger)currentTime{ 
// HURRAY! We know what the currentTime is :) 
} 
! 
//.. 
@end
A parting note… 
with great SUPER power 
comes great SUPER 
responsibility!
Questions? 
HARIS AMIN 
Cofounder/CO-CTO @itsglimpse 
@harisamin

More Related Content

Viewers also liked

Packet sniffing in switched LANs
Packet sniffing in switched LANsPacket sniffing in switched LANs
Packet sniffing in switched LANsIshraq Al Fataftah
 
Arp (address resolution protocol)
Arp (address resolution protocol)Arp (address resolution protocol)
Arp (address resolution protocol)tigerbt
 
Address resolution protocol
Address resolution protocolAddress resolution protocol
Address resolution protocolasimnawaz54
 
Packet sniffing & ARP Poisoning
 Packet sniffing & ARP Poisoning  Packet sniffing & ARP Poisoning
Packet sniffing & ARP Poisoning Viren Rao
 
Writing A Health Research Proposal
Writing A Health Research ProposalWriting A Health Research Proposal
Writing A Health Research ProposalSoha Rashed
 
Address Resolution Protocol
Address Resolution ProtocolAddress Resolution Protocol
Address Resolution ProtocolRam Dutt Shukla
 
My research proposal.ppt
My research proposal.pptMy research proposal.ppt
My research proposal.pptnanimamat
 
The Research Proposal
The Research ProposalThe Research Proposal
The Research Proposalguest349908
 

Viewers also liked (9)

Dynamic Port Scanning
Dynamic Port ScanningDynamic Port Scanning
Dynamic Port Scanning
 
Packet sniffing in switched LANs
Packet sniffing in switched LANsPacket sniffing in switched LANs
Packet sniffing in switched LANs
 
Arp (address resolution protocol)
Arp (address resolution protocol)Arp (address resolution protocol)
Arp (address resolution protocol)
 
Address resolution protocol
Address resolution protocolAddress resolution protocol
Address resolution protocol
 
Packet sniffing & ARP Poisoning
 Packet sniffing & ARP Poisoning  Packet sniffing & ARP Poisoning
Packet sniffing & ARP Poisoning
 
Writing A Health Research Proposal
Writing A Health Research ProposalWriting A Health Research Proposal
Writing A Health Research Proposal
 
Address Resolution Protocol
Address Resolution ProtocolAddress Resolution Protocol
Address Resolution Protocol
 
My research proposal.ppt
My research proposal.pptMy research proposal.ppt
My research proposal.ppt
 
The Research Proposal
The Research ProposalThe Research Proposal
The Research Proposal
 

Similar to Intro to Obj-C Design Patterns or Or how I learned to be less bad

iOS Development: What's New
iOS Development: What's NewiOS Development: What's New
iOS Development: What's NewNascentDigital
 
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsFI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsPetr Dvorak
 
Getting started with titanium
Getting started with titaniumGetting started with titanium
Getting started with titaniumNaga Harish M
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CDataArt
 
Getting started with Appcelerator Titanium
Getting started with Appcelerator TitaniumGetting started with Appcelerator Titanium
Getting started with Appcelerator TitaniumTechday7
 
MFF UK - Introduction to iOS
MFF UK - Introduction to iOSMFF UK - Introduction to iOS
MFF UK - Introduction to iOSPetr Dvorak
 
iPhone dev intro
iPhone dev introiPhone dev intro
iPhone dev introVonbo
 
Beginning to iPhone development
Beginning to iPhone developmentBeginning to iPhone development
Beginning to iPhone developmentVonbo
 
Bootstrapping iPhone Development
Bootstrapping iPhone DevelopmentBootstrapping iPhone Development
Bootstrapping iPhone DevelopmentThoughtWorks
 
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろうUnity Technologies Japan K.K.
 
CoconutKit
CoconutKitCoconutKit
CoconutKitdefagos
 
Objective-C Is Not Java
Objective-C Is Not JavaObjective-C Is Not Java
Objective-C Is Not JavaChris Adamson
 
Reactive summit 2020 microsoft orleans the easy way
Reactive summit 2020   microsoft orleans the easy wayReactive summit 2020   microsoft orleans the easy way
Reactive summit 2020 microsoft orleans the easy wayJohn Azariah
 
Wahckon[2] - iOS Runtime Hacking Crash Course
Wahckon[2] - iOS Runtime Hacking Crash CourseWahckon[2] - iOS Runtime Hacking Crash Course
Wahckon[2] - iOS Runtime Hacking Crash Courseeightbit
 
Objective-C talk
Objective-C talkObjective-C talk
Objective-C talkbradringel
 
Cappuccino - A Javascript Application Framework
Cappuccino - A Javascript Application FrameworkCappuccino - A Javascript Application Framework
Cappuccino - A Javascript Application FrameworkAndreas Korth
 
Design Patterns in iOS
Design Patterns in iOSDesign Patterns in iOS
Design Patterns in iOSYi-Shou Chen
 

Similar to Intro to Obj-C Design Patterns or Or how I learned to be less bad (20)

iOS Development: What's New
iOS Development: What's NewiOS Development: What's New
iOS Development: What's New
 
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsFI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS Basics
 
Getting started with titanium
Getting started with titaniumGetting started with titanium
Getting started with titanium
 
Ios development
Ios developmentIos development
Ios development
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-C
 
Wordpress Shortcode
Wordpress ShortcodeWordpress Shortcode
Wordpress Shortcode
 
Getting started with Appcelerator Titanium
Getting started with Appcelerator TitaniumGetting started with Appcelerator Titanium
Getting started with Appcelerator Titanium
 
MFF UK - Introduction to iOS
MFF UK - Introduction to iOSMFF UK - Introduction to iOS
MFF UK - Introduction to iOS
 
iPhone dev intro
iPhone dev introiPhone dev intro
iPhone dev intro
 
Beginning to iPhone development
Beginning to iPhone developmentBeginning to iPhone development
Beginning to iPhone development
 
Bootstrapping iPhone Development
Bootstrapping iPhone DevelopmentBootstrapping iPhone Development
Bootstrapping iPhone Development
 
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
 
CoconutKit
CoconutKitCoconutKit
CoconutKit
 
Objective-C Is Not Java
Objective-C Is Not JavaObjective-C Is Not Java
Objective-C Is Not Java
 
Reactive summit 2020 microsoft orleans the easy way
Reactive summit 2020   microsoft orleans the easy wayReactive summit 2020   microsoft orleans the easy way
Reactive summit 2020 microsoft orleans the easy way
 
Wahckon[2] - iOS Runtime Hacking Crash Course
Wahckon[2] - iOS Runtime Hacking Crash CourseWahckon[2] - iOS Runtime Hacking Crash Course
Wahckon[2] - iOS Runtime Hacking Crash Course
 
Objective-C talk
Objective-C talkObjective-C talk
Objective-C talk
 
Cappuccino - A Javascript Application Framework
Cappuccino - A Javascript Application FrameworkCappuccino - A Javascript Application Framework
Cappuccino - A Javascript Application Framework
 
React nativebeginner1
React nativebeginner1React nativebeginner1
React nativebeginner1
 
Design Patterns in iOS
Design Patterns in iOSDesign Patterns in iOS
Design Patterns in iOS
 

Recently uploaded

Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 

Recently uploaded (20)

Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 

Intro to Obj-C Design Patterns or Or how I learned to be less bad

  • 1. Intro to Obj-C Design Patterns Or how I learned to be less bad Haris Amin
  • 2. GLIMPSE A fun way to meet new people through Instagram
  • 3. Design Patterns • Why do we care? • Let’s explore two design patterns in Cocoa 1. Notification Center 2. Delegates
  • 4. Notification Center • NSNotificationCenter class • Easiest to learn (IMHO) but also easiest to abuse • Very flexible
  • 5. Notification Center • Let’s Grab a NotificationCenter ‘instance’ [NSNotificationCenter defaultCenter] ! • Then PUBLISH a specific message (2 ways) 1. With content 2. Without Content
  • 6. Publish Message Without Content • We want to notify that something specific ‘happened’ • Method Signature - (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender
  • 7. Publish Message Without Content - (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender • E.G. The Video’s current time just updated [[NSNotificationCenter defaultCenter] postNotificationName:@"TimeUpdated" object:self];
  • 8. Publish Message With Content - (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo • E.G. The Video’s current time just updated WITH the actual current time [[NSNotificationCenter defaultCenter] postNotificationName:@"TimeUpdated" object:self userInfo:@{@"currentTime": 20}];
  • 9. Subscribing to a Message - (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender • In our VideoPlayer class we will just subscribe to the NotificationCenter Message [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTimeUpdate:) name:@"TimeUpdated" object:nil]; - (void)handleTimeUpdate:(NSNotification *)note{ // note.userInfo is the CONTENT we passed // ... }
  • 10. Notification Center… THE GOOD • Really flexible • Don’t have to define any sort of protocol for your messages • You can subscribe and publish messages in any number of places
  • 11. Notification Center… THE BAD • TOO flexible?! • Don’t have to define any sort of protocol for your messages • You can subscribe and publish messages in any number of of places
  • 12. Delegates • Very solid pattern • Vigorously multitude of Cocoa classes (UITableView, UICollectionView, UIAlterView.. etc.) • You have to define the protocol of the messages you send
  • 13. A Personal Note… • I consumed delegates without really understanding how they work • I used to be scared of trying to implement one myself
  • 14. TIME TO GAIN A SUPERPOWER!
  • 15. Delegates • Very solid pattern • Vigorously multitude of Cocoa classes (UITableView, UICollectionView, UIAlterView.. etc.) • You have to define the protocol of the messages you send
  • 16. Define a Delegate Protocol • Very solid pattern • Vigorously multitude of Cocoa classes (UITableView, UICollectionView, UIAlterView.. etc.) • You have to define the protocol of the messages you send
  • 17. Define Protocol @protocol VideoDelegate; ! @interface Video: NSObject ! @property (nonatomic, weak) id<VideoDelegate> delegate; ! @end ! @protocol VideoDelegate <NSObject> @required //... ! @optional -(void)video:(Video *)video updatedTime:(NSUInteger)currentTime; ! @end // end of delegate protocol
  • 18. Delegating Methods in the Protocol • In our Video class, where need be, we will delegate respective messages @implementation Video //.. ! if (self.delegate && [self.delegate respondsToSelector:@selector(video:updatedTime)]) { [self.delegate video:updatedTime]; } //.. @end
  • 19. Implement Protocol #include "Video.h" // needed to include the @delegate protocol info ! @interface VideoPlayer : UIViewController <VideoDelegate> @end ! @implementation VideoPlayer //.. ! -(void)video:(Video *)video updatedTime:(NSUInteger)currentTime{ // HURRAY! We know what the currentTime is :) } ! //.. @end
  • 20. A parting note… with great SUPER power comes great SUPER responsibility!
  • 21. Questions? HARIS AMIN Cofounder/CO-CTO @itsglimpse @harisamin