PowenKo, iOS Tutorial 110-3, Framework,Create your own Delegate.

Create your own Delegate.

In MainClass.h

//Include the sub class header:
#import "MySubClassName.h"

//Add the delegate as an input to the class
@interface MainClassViewController : UIViewController
	MySubClassName *mySubClass1;

In MainClass.m

//When the create the sub class:
	mySubClass1 = [[MySubClassName alloc] init];
	[mySubClass1 setDelegate:self];

//The method that will be called back:
- (void)MethodNameToCallBack:(NSString *)s

//********** DEALLOC **********
- (void)dealloc
	[mySubClass1 release];

In SubClass.h

#import <Foundation/Foundation.h>

//Define the protocol for the delegate
@protocol SubClassDelegate
- (void)MethodNameToCallBack:(NSString *)s;

@interface MySubClassName : NSObject
	id <SubClassDelegate> delegate;

@property (nonatomic, assign) id  <SubClassDelegate> delegate;

In SubClass.m

@implementation MySubClassName
//Synthesize the delegate property:
@synthesize delegate;

//When you want to callback the MainClass method:
	[[self delegate] MethodNameToCallBack:@"Hello World"];

sample code:
Tutorial_framework_ Crate_delegate

Leave a Reply