技术交流QQ群:414971585

这篇文章建议和前一篇一起看, 另外先弄清楚IOS的block是神马东东。

委托和block是IOS上实现回调的两种机制。Block基本可以代替委托的功能,而且实现起来比较简洁,比较推荐能用block的地方不要用委托。

本篇的demo和前一篇是同一个,可以到github上下载不同的版本, 源码下载地址:

https://github.com/pony-maggie/DelegateDemo

A类(timeControl类)的头文件先要定义block,代码如下:

 
  1. //委托的协议定义
  2. @protocol UpdateAlertDelegate
  3. - (void)updateAlert:(NSString *tltle);
  4. @end
  5. @interface TimerControl : NSObject
  6. //委托变量定义
  7. @property (nonatomic, weak) id delegate;
  8. //block
  9. typedef void (^UpdateAlertBlock)(NSString *tltle);
  10. @property (nonatomic, copy) UpdateAlertBlock updateAlertBlock;
  11. - (void) startTheTimer;
  12. @end

A类的实现文件,原来用委托的地方改成调用block:

 
  1. - (void) timerProc
  2. {
  3. //[self.delegate updateAlert:@"this is title"];//委托更新UI
  4. //block代替委托
  5. if (self.updateAlertBlock)
  6. {
  7. self.updateAlertBlock(@"this is title");
  8. }
  9. }

再来看看视图类,实现block即可:

 
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view, typically from a nib.
  5. TimerControl *timer = [[TimerControl alloc] init];
  6. timer.delegate = self; //设置委托实例
  7. //实现block
  8. timer.updateAlertBlock = ^(NSString *title)
  9. {
  10. UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:@"时间到" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
  11. alert.alertViewStyle=UIAlertViewStyleDefault;
  12. [alert show];
  13. };
  14. [timer startTheTimer];//启动定时器,定时5触发
  15. }  

    //"被委托对象"实现协议声明的方法,由"委托对象"调用

    - (void)updateAlert:(NSString *title)

    {

    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:@"时间到" delegate:self cancelButtonTitle:nilotherButtonTitles:@"确定",nil];

    alert.alertViewStyle=UIAlertViewStyleDefault;

    [alert show];

    }

iOS 传值 委托(delegate)和block 对比的更多相关文章

  1. iOS基础之顺传逆传传值(delegate、block)

    写给iOS新手的福利! 在项目中经常会用到传值,根据传值的方向分为顺传(从根控制器到子控制器)和逆传(从子控制器到根控制器).在这里写了个Demo简单演示了效果,创建了两个控制器: 一个为根控制器,一 ...

  2. iOS开发-委托(Delegate)浅谈

    委托其实并不是OC中才有,C#中也有,不过彼此的理解方式是不一样的,OC中委托是协议的一种,需要使用@protocol声明,委托一般在iOS开发中页面中传值用的比较多.委托是Cocoa中最简单.最灵活 ...

  3. ios 设置委托delegate

    为了进行页面传值,也可以用委托的方法. 下面以时间控件为例. 1.首先,在.h 文件设置委托 #import <UIKit/UIKit.h> @protocol DatePickerVie ...

  4. ios多播委托

    在现实中回调的需求也分两种 一对一的回调. 一对多的回调. 对于一对一的回调,在IOS中使用delegate.block都能实现.而一对多的回调基本就是通知中心了. 假如现在有一个需求,我们以图片下载 ...

  5. IOS 多播委托(GCDMulticastDelegate)

    原文:http://www.cnblogs.com/dagehaoshuang/p/4043264.html 在IOS中为了实现回调一般有如下几个方法: delegate 通知中心 block KVO ...

  6. iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值实现方法:1.通过设置属性,实现页面间传值:2.委托delegate方式:3.通知notification方式:4.block方式:5.UserDefault或者文件方式:6.单例模式 ...

  7. iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)

    iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...

  8. 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错

    原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...

  9. iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)   iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...

随机推荐

  1. HDU 4067 hdoj 4067 Random Maze 最小费用流

    给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...

  2. Cheap Hollister Clothing

    (link to hollisterco site), Spectacles don't simply take care of the eye area inside sun; Putting th ...

  3. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

  4. C++模拟C#事件委托机制(二)

    原文 来自于http://www.cnblogs.com/netssfy/archive/2010/02/02/1662056.html 为了解决非法地址访问的冲突,首先需要知道发生该错误的原因是什么 ...

  5. python 中的高级函数reduce()

    reduce()函数也是Python内置的一个高阶函数.reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接收 ...

  6. ASP.NET简单实现APP中用户个人头像上传和裁剪

    最近有个微信项目的用户个人中心模块中,客户要求用户头像不仅仅只是上传图片,还需要能对图片进行裁剪.考虑到flash在IOS和Android上的兼容性问题,于是想着能从js这块入手,在网上发现了devo ...

  7. C#编程:SqlCommand.Parameters.Add()方法的参数问题。

    在存储过程中添加2个参数 sql语句 例: “update [tablename] username = @username where id=@id” 然后把需要的 command.Paramete ...

  8. C#如何获取物理网卡,虚拟网卡,以及无线网卡

    就不废话了,直接上代码 /// <summary></summary> /// 显示本机各网卡的详细信息 /// <summary></summary> ...

  9. TCPCopy使用

      http://www.thinkingbar.com/2014/04/17/tcpcopy使用/ 主题 技术 一.应用背景 主要用于系统的稳定性测试.它可以复制线上服务器的请求,通过修改TCP/I ...

  10. php大力力 [050节] 兄弟连高洛峰 PHP教程 2014年[数据库、PDO教程]

    php大力力 [050节] 兄弟连高洛峰 PHP教程 2014年[数据库.PDO教程] 第14章 数据库252.[2014]兄弟连高洛峰 PHP教程14.1.1 复习数据库[已发布,点击下载]253. ...