iOS 传值 委托(delegate)和block 对比
技术交流新QQ群:414971585
这篇文章建议和前一篇一起看, 另外先弄清楚IOS的block是神马东东。
委托和block是IOS上实现回调的两种机制。Block基本可以代替委托的功能,而且实现起来比较简洁,比较推荐能用block的地方不要用委托。
本篇的demo和前一篇是同一个,可以到github上下载不同的版本, 源码下载地址:
https://github.com/pony-maggie/DelegateDemo
A类(timeControl类)的头文件先要定义block,代码如下:
- //委托的协议定义
- @protocol UpdateAlertDelegate
- - (void)updateAlert:(NSString *tltle);
- @end
- @interface TimerControl : NSObject
- //委托变量定义
- @property (nonatomic, weak) id delegate;
- //block
- typedef void (^UpdateAlertBlock)(NSString *tltle);
- @property (nonatomic, copy) UpdateAlertBlock updateAlertBlock;
- - (void) startTheTimer;
- @end
A类的实现文件,原来用委托的地方改成调用block:
- - (void) timerProc
- {
- //[self.delegate updateAlert:@"this is title"];//委托更新UI
- //block代替委托
- if (self.updateAlertBlock)
- {
- self.updateAlertBlock(@"this is title");
- }
- }
再来看看视图类,实现block即可:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- TimerControl *timer = [[TimerControl alloc] init];
- timer.delegate = self; //设置委托实例
- //实现block
- timer.updateAlertBlock = ^(NSString *title)
- {
- UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:@"时间到" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
- alert.alertViewStyle=UIAlertViewStyleDefault;
- [alert show];
- };
- [timer startTheTimer];//启动定时器,定时5触发
- }
//"被委托对象"实现协议声明的方法,由"委托对象"调用
- (void)updateAlert:(NSString *title)
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:@"时间到" delegate:self cancelButtonTitle:nilotherButtonTitles:@"确定",nil];
alert.alertViewStyle=UIAlertViewStyleDefault;
[alert show];
}
iOS 传值 委托(delegate)和block 对比的更多相关文章
- iOS基础之顺传逆传传值(delegate、block)
写给iOS新手的福利! 在项目中经常会用到传值,根据传值的方向分为顺传(从根控制器到子控制器)和逆传(从子控制器到根控制器).在这里写了个Demo简单演示了效果,创建了两个控制器: 一个为根控制器,一 ...
- iOS开发-委托(Delegate)浅谈
委托其实并不是OC中才有,C#中也有,不过彼此的理解方式是不一样的,OC中委托是协议的一种,需要使用@protocol声明,委托一般在iOS开发中页面中传值用的比较多.委托是Cocoa中最简单.最灵活 ...
- ios 设置委托delegate
为了进行页面传值,也可以用委托的方法. 下面以时间控件为例. 1.首先,在.h 文件设置委托 #import <UIKit/UIKit.h> @protocol DatePickerVie ...
- ios多播委托
在现实中回调的需求也分两种 一对一的回调. 一对多的回调. 对于一对一的回调,在IOS中使用delegate.block都能实现.而一对多的回调基本就是通知中心了. 假如现在有一个需求,我们以图片下载 ...
- IOS 多播委托(GCDMulticastDelegate)
原文:http://www.cnblogs.com/dagehaoshuang/p/4043264.html 在IOS中为了实现回调一般有如下几个方法: delegate 通知中心 block KVO ...
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值实现方法:1.通过设置属性,实现页面间传值:2.委托delegate方式:3.通知notification方式:4.block方式:5.UserDefault或者文件方式:6.单例模式 ...
- iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...
- 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错
原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...
- iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...
随机推荐
- Add Two Numbers (c#)
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Javascript跨域
跨域:只要协议.域名或端口有任何一个不同,就定义为跨域. CORS(Cross-Origin Resource Sharing):即跨域资源共享.定义了在访问跨域资源时,浏览器和服务器该如何沟通.CO ...
- Scala的第一步
第一步:学习使用Scala解释器 开始Scala最简单的方法是使用Scala解释器,它是一个编写Scala表达式和程序的交互式“shell”.在使用Scala之前需要安装Scala,可以参考 Firs ...
- Java泛型学习笔记 - (二)泛型类
1. 我们先写一个没有泛型的类Box: public class Box { private Object obj; public Box() {} public Object getObj() { ...
- freeCAD特性列表
通用特性 基本应用 FreeCAD 是跨平台的. 它在 Windows Linux 和 Mac OSX 等平台上运行表现一致. FreeCAD 是图形化应用程序. FreeCAD 基于著名的 GUI ...
- leetcode Online Judge 150题 解答分析之一 Reverse Words in a String
问题 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- 2016 - 2 - 20 ARC知识总结(二 autorelease概念及实现)
首先祝自己生日快乐~23咯~ 一 autorelease的概念 autorelease会像C语言的自动变量那样来对待对象实例.当超出作用域(相当于变量作用域)时,对象的实例release实力方法被调 ...
- Three.js typescript definitely typed 文件
最近学习three.js,想用typescript编写代码,去http://definitelytyped.org/找了一圈没有发现three.js的definitely typed文件. 好吧,花了 ...
- Android 与Unity交互之Toast消息
Toast.makeText(MainActivity.this,message.obj.toString(),Toast.LENGTH_SHORT).show();这一句代码不能直接放在 publi ...
- Maven项目下java.lang.ClassNotFoundException的解决方法
问题背景: Maven的project下,项目中已经引用了相应的jar包.Java class中没有语法错误,在执行时报ClassNotFound.检查了Maven的pom.xml,依赖引入正常. 错 ...