当前位置:首页 > 通信资讯 > 正文

ios target-action(target-action机制)

IOS中的target action控件的实现

实现代码:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #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
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #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里面

?
1 2 3 4 5 6 7 8 9 10 -(void)control_dosomthing{ NSLog(@"control_dosomthing"); } - (void)viewDidLoad { [super viewDidLoad]; Sample *sample1 = [Sample new]; [sample1 addTarget:self action:@selector(control_dosomthing)]; }

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

原文链接:http://lizhuang.iteye.com/blog/1910669

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。