实现iOS中的target action控件

#import <Foundation/Foundation.h>


@interface Sample : NSObject {

	SEL action;
	id  target;
	
}
@property SEL action;
@property (assign) id target;

-(void)addTarget:(id) t action:(SEL) s;
-(void)sample_dosomthing;

@end


#import "Sample.h"


@implementation Sample

@synthesize action;
@synthesize target;


-(void)dealloc{
	target = nil;
	[super dealloc];
}

-(void)addTarget:(id) t action:(SEL) s{
	self.action = s;
	self.target = t;
}


-(void)sample_dosomthing{
	
	[self.target performSelector:self.action];
}

@end



这是一个例子,在一个UIViewController里面

-(void)control_dosomthing{
	NSLog(@"control_dosomthing");
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	Sample *sample1 = [Sample new];
	[sample1 addTarget:self action:@selector(control_dosomthing)];
}

猜你喜欢

转载自lizhuang.iteye.com/blog/1910669