封装Tatget Action

@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

=======

@interface Other : NSObject {

}

-(void)other_dosomthing;
@end

-------------------------

#import "Other.h"


@implementation Other

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

@end

=============

Sample *sample1 = [Sample new];
    [sample1 addTarget:self action:@selector(control_dosomthing)];
   
    [sample1 sample_dosomthing]; //SEE LOG
   
    [sample1 release];
   
    //////////////////////////////////////////////////////////////////
   
    Sample *sample2 = [Sample new];
    Other *other = [Other new];
    [sample2 addTarget:other action:@selector(other_dosomthing)];
   
    [sample2 sample_dosomthing]; //SEE LOG
   
    [other release];
    [sample2 release];

posted @ 2011-09-14 16:03  Gang.Wang  阅读(372)  评论(0编辑  收藏  举报