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

September 13, 2011   ·   0 Comments

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
	<SubClassDelegate>
{
	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
{
	NSLog(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;
@end

@interface MySubClassName : NSObject
{
	id <SubClassDelegate> delegate;

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

@end
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
shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
iOS Tutorial9

Amazon

adsbygoogle

Recent Posts

Categories

Amazon