Protocols are another important feature provided by Objective-C language. Basically protocols are used for a particular situation according to the need of a program. Within protocol methods will be declared which are expected to be used for a particular task. Protocols includes two types of methods required and optional. According to the names we will get to know that the methods under required part will be necessary to implement where as we can skip the definition part of optional methods.
We can create protocol in this way :-
@protocol DemoProtocolName
@required
//all required methods come under this part of protocol
@optional
//all optional methods come under this part of protocol
@end
With the help of an program we will understand the concept of protocols in objective-C language in which we simply display a hello message with the help of protocol. First of all we will create a file with NSObject as superclass and we will create protocol in that file. Suppose that file name is DemoProtocolModel.h now in this file we will create a protocol.
DemoProtocolModel.h
#import <Foundation/Foundation.h>
@protocol DemoProtocol <NSObject>
@required
-(void)printHelo:(NSString *)dataString;//this is a required method so we have to implement this method.
@end
@interface DemoProtocolModel : NSObject
{
id delegate; //variable of id type
}
-(void)setDelegate:(id)delegate;// method declaration to set delegate to use protocol
-(void)getDataToPrintHelo:(NSString *)dataString; //method which will help in providing data which we need to display to the required method
@end
DemoProtocolModel.m
#import "DemoProtocolModel.h"
#import "DeclarationOfStrings.h//this class includes the declaration of data string
@implementation DemoProtocolModel
- (void) setDelegate:(id)newDelegate{
delegate = newDelegate;
}
-(void)getDataToPrintHelo
{
[delegate printHelo:data];//Providing value to the required method with the help of delegate.Here the data which is passed to the prindhelo method is set in a separate file which includes all the NSString values which we can use anywhere in the project.
}
@end
File which is used to declare and define the constant will be created in the following way:-
Super class will be NSObject and in the interface part of file we will declare the strings in this manner:-
DeclarationOfStrings.h
#import <Foundation/Foundation.h>
@interface DeclarationOfStrings : NSObject
extern NSString *const data;
@end
In the implementation part of that file we will provide values to the strings.
DeclarationOfStrings.m
#import "DeclarationOfStrings.h"
@implementation DeclarationOfStrings
NSString * const data=@"This is required method";
@end
Now if we want to show the output at button click so we will take a button on view controller from object gallery and create an action of that button .
ViewController.h
#import <UIKit/UIKit.h>
#import "DemoProtocolModel.h"
@interface ViewController : UIViewController
- (IBAction)btnShowMessage:(id)sender; //Action of button
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
{
DemoProtocolModel *message;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
message=[[DemoProtocolModel alloc]init]; //allocate memory to object of DemoProtocolModel class
[message setDelegate:self]; //set delegate to use protocol
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)printHelo:(NSString*)dataString
{
NSLog(@"%@",dataString); //display the message which is provided inside data string
}
- (IBAction)btnShowMessage:(id)sender {
[message getDataToPrintHelo]; //on button click this method will get called and inside this method required method will get called and then we will get the output.
}
@end
Output after debug this program will be display in this manner:-
2016-05-16 11:53:20.625 protocol[1304:46659] This is required method
So this is the very simple example of how to use protocols in the program and how the concept of protocol works.
0 Comment(s)