Block - 6
2015-10-27 11:53
330 查看
学习完Block 1 - 5 相信对block 的理解和使用肯定是不存在什么问题的,但是提到一点,如何用block代替delegate,本篇将会学习到:
本文学习于 :https://github.com/pony-maggie/DelegateDemo (若侵告知必删)
分别实现block 和 delegate 的类:
头文件:
delegate实现:
---------------------分割线
block实现:
看完例子,对它们的区别一目了然吧!block更加简洁,直观,灵活! 本质上其实都是用了“反射“的原理。结构上也是非常相像,例如判断处,和实现处。
除了使用上的简洁简便上,最重要的一点是拜托了delegate使用上必须对象化的枷锁,delegate声明代理,实现,设置代理,维护协议等等繁琐并且与对象相关度强的麻烦,在block上并可以一一纫解,这也是block灵活的强大之处。
本文学习于 :https://github.com/pony-maggie/DelegateDemo (若侵告知必删)
分别实现block 和 delegate 的类:
头文件:
#import <Foundation/Foundation.h> //委托的协议定义 @protocol UpdateAlertDelegate <NSObject> - (void)updateAlert; @end @interface TimerControl : NSObject //委托变量定义 @property (nonatomic, weak) id<UpdateAlertDelegate> delegate; //block typedef void (^UpdateAlertBlock)(); @property (nonatomic, copy) UpdateAlertBlock updateAlertBlock; - (void) startTheTimer; @end
delegate实现:
#import "TimerControl.h" @implementation TimerControl - (void) startTheTimer { [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(timerDelegateProc) userInfo:nil repeats:NO]; } - (void) timerDelegateProc { if ([self.delegate respondsToSelector:@selector(updateAlert)]) { [self.delegate updateAlert]; } } @end
#import "DelegateDemoViewController.h" @interface DelegateDemoViewController () <UpdateAlertDelegate> @end @implementation DelegateDemoViewController - (void)viewDidLoad { [super viewDidLoad]; TimerControl *timer = [[TimerControl alloc] init]; timer.delegate = self; //设置委托实例 [timer startTheTimer];//启动定时器,定时5触发 } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } //"被委托对象"实现协议声明的方法,由"委托对象"调用 - (void)updateAlert { UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"时间到" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil]; alert.alertViewStyle=UIAlertViewStyleDefault; [alert show]; } @end
---------------------分割线
block实现:
#import "TimerControl.h" @implementation TimerControl - (void) startTheTimer { [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(timerBlockProc) userInfo:nil repeats:NO]; } - (void) timerBlockProc { //block代替委托 if (self.updateAlertBlock) { self.updateAlertBlock(); } } @end
#import "DelegateDemoViewController.h" @interface DelegateDemoViewController () @end @implementation DelegateDemoViewController - (void)viewDidLoad { [super viewDidLoad]; TimerControl *timer = [[TimerControl alloc] init]; //实现block timer.updateAlertBlock = ^() { UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"时间到" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil]; alert.alertViewStyle=UIAlertViewStyleDefault; [alert show]; }; [timer startTheTimer];//启动定时器,定时5触发 } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
看完例子,对它们的区别一目了然吧!block更加简洁,直观,灵活! 本质上其实都是用了“反射“的原理。结构上也是非常相像,例如判断处,和实现处。
除了使用上的简洁简便上,最重要的一点是拜托了delegate使用上必须对象化的枷锁,delegate声明代理,实现,设置代理,维护协议等等繁琐并且与对象相关度强的麻烦,在block上并可以一一纫解,这也是block灵活的强大之处。
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Ruby中Block和迭代器的使用讲解
- Ruby中使用Block、Proc、lambda实现闭包
- Ruby中的block、proc、lambda区别总结
- C#使用委托(delegate)实现在两个form之间传递数据的方法
- Query中click(),bind(),live(),delegate()的区别
- jQuery中 delegate使用的问题
- 浅谈jquery中delegate()与live()
- 由点击页面其它地方隐藏div所想到的jQuery的delegate
- jQuery中delegate与on的用法与区别示例介绍
- JS delegate与live浅析
- jQuery事件 delegate()使用方法介绍
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法