Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@between40and2
Last active December 16, 2015 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save between40and2/5441818 to your computer and use it in GitHub Desktop.
Save between40and2/5441818 to your computer and use it in GitHub Desktop.
iOS-Callback
Here is my sum on which callback mechanism should be used for which cases.
- for UIView and UIVC, use delegate
- for model change, use NSNotification or KVO. However, KVO is not designed for communicating "events", therefore NSNotif is the preferred.
- for Data Access, use block.
for UIControl subclass, use target-action.
Bye Delegate, Hello Block
____
Delegate is rigid, to make loose coupling relationship via delegate, you need certain boilerplate code, espeically the declaration on the interface in header (.h) files. (I mean, @interface MyClass: NSObject <YourProtocol> )
Block is flexible.
Whether you use delegate or block is an issue of style on coding. Since it is about the style or the form, it is secondary concern, compared with the first-order/primary concern such as how to make something happen right - the functionality. However, I would argue that since the primary is what everyone must do, then the secondary becomes the differential factor that whose code is better.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment