
Delegation Pattern in Objective-C
It's been a long time since I had to work with old good Objective-C, the king of brackets [[[[]]]]]]]. I recently had to revisit implementation of one of the fundamental design pattern in iOS, called Delegation.
According to Apple's documentation:
"Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled."
So how can it be implemented in Objective-C?
In this example:
- AppDelegate class is acting as receiver of delegated events
- TapProtocol defines the methods that have to be implemented by classes that are conforming to the protocol
- ViewController receives events from the button and passes it to delegate.
TapProtocol.h
#import <Foundation/Foundation.h>
@protocol TapProtocol <NSObject>
-(void) buttonTapped;
@end
AppDelegate.h
Notice how we it's specified that this class conforms to the TapProtocol:
@interface AppDelegate : UIResponder <UIApplicationDelegate, TapProtocol>
#import <UIKit/UIKit.h>
#import "TapProtocol.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate, TapProtocol>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m:
Code below indicates that we want AppDelegate to act as a delegate of ViewController's tap delegate protocol. In other words events from ViewController will be passed to this class. In order for this to happen you have to implement the methods specified by protocol:
- (void)buttonTapped{
NSLog(@"Button Tapped Here");
}
ViewController * vc = (ViewController *)[[[[UIApplication sharedApplication] delegate]window] rootViewController];
vc.delegate = self;
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController * vc = (ViewController *)[[[[UIApplication sharedApplication] delegate]window] rootViewController];
vc.delegate = self;
return YES;
}
#pragma mark Delegate methods
- (void)buttonTapped{
NSLog(@"Button Tapped Here");
}
@end
Finally in ViewController you have to implement following code.
The crucial statements are:
@property (weak, nonatomic) id<TapProtocol> delegate; declaring delegate.
[self.delegate buttonTapped]; Sending messages to delegate.
ViewController.h
#import <UIKit/UIKit.h>
@protocol TapProtocol;
@interface ViewController : UIViewController
@property (weak, nonatomic) id<TapProtocol> delegate;
@end
ViewController.m
#import "ViewController.h"
#import "TapProtocol.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)tapTap:(id)sender {
[self.delegate buttonTapped];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)buttonTapped:(id)sender {
[self.delegate buttonTapped];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Leave Delegation Pattern in Objective-C to:
Read more #programming posts
Best Posts From cryptoizotx
We have not curated any of cryptoizotx's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From cryptoizotx
- Keyframe animations with Swift
- One algorithm a day: Keeping track of the largest element in a stack
- One algorithm a day. Write a function that returns best profit you could made from trading.
- One algorithm a day: floor of the square root of the input
- iOS Interview: Delegation Pattern in Swift
- Delegation Pattern in Objective-C
- Swift Interview Questions Recursion: Print numbers from 0 to n without using for loop.
- Bitwise Operators in Swift
- S O L I D Principle Rules in iOS
- Decentralized Programmer: Solidity 101
- Decentralized Programmer: My first Erc20 token on Ethereum blockchain!
- How to decentralize your resume
- Decentralized Programmer: Simple KuCoin Trading Bot
- Decentralized Programmer Part 6 My very first Dapp!
- Decentralized Programmer Part 5 Deployment of your contract with Remix IDE!
- Decentralized Programmer Part 4 web3 and specifying value of the transaction
- Decentralized Programmer: Part 3 Remix IDE + Ganache-CLI
- Decentralized Programmer: Part 2 Truffle
- Decentralized Programmer: Part 1 Environment Metamask and Ganache
- Crypto Market Sentiment Update, December 21, 2017