IOS中的target action控件的实现

时间:2019-03-31
本文章向大家介绍IOS中的target action控件的实现,主要包括IOS中的target action控件的实现使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

以上就是IOS中的target action控件的实现,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!