Skip to main content

How to create Custom delegate method in iOS

Delegate is used for send receive data between two viewcontroller.

For example if We have two View controllers namely FirstViewController and

SecondViewController

Step :1 Add the following line of code to FirstViewController.h file

#import <UIKit/UIKit.h>

@protocol MyDelegate

-(void)MyDelegateMethod:(NSString *)description;

@end

@interface FirstViewController : UIViewController

{



}

@property (nonatomic, weak) id MyDelegate;

Step 2 : To implement the above created delegate method one needs to add the below

mentioned code in SecondViewController.m file

@interface SecondViewController()<MyDelegate>

@end

@implementation SecondViewController

-(void)GoToFirst

{

FirstViewController *first = [[FirstViewController

alloc]initWithNibName:@"FirstViewController" bundle:Nil];

first.MyDelegate = self;

[self.navigationController pushViewController:cart animated:YES];

}

// Delegate Method Goes Here

-(void)MyDelegateMethod:(NSString *)description;

{

}

@end

 If you have any further doubts, drop us an email at info@infigic.com and we’ll get back to you with the best possible solution. Infigic is a  Mobile Application Development Company and we are always there to solve your queries. 

Comments