IOSにおけるtarget actionコントロールの実装

1353 ワード

IOSにおけるtarget actionコントロールの実装
実装コード:

#import  
 
 
@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)]; 
} 

以上はIOSの中のtarget actionコントロールの実現で、もし疑問があれば伝言あるいは当駅のコミュニティに行って討論を交流して、読書に感謝して、みんなを助けることができることを望んで、みんなの当駅に対する支持に感謝します!