IOS

关注公众号 jb51net

关闭
首页 > 软件编程 > IOS > iOS target action

IOS中的target action控件的实现

作者:lizhuang

这篇文章主要介绍了IOS中的target action控件的实现的相关资料,这里提供实现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控件的实现,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:
阅读全文