1.Delegate(代理、委托) 代理几乎是iOS开发中最经常使用的传值方式,在项目中的AppDelegate就是使用的这种设计模式,不只如此,还有不少原生的控件也使用的这种设计模式,好比:UITextFiled,UITableView等等。官方给出的解释以下:html
Delegation is a simple and powerful pattern in which one object in a program 1 acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.ios
具体使用方法再也不示例(网上关于Delegate基础使用的博文数不胜数示例),主要总结一下Delegate的优势和缺陷。设计模式
优势: 1.减小代码的耦合性,使事件监听和事件处理相分离。 2.清晰的语法定义,减小维护成本,较强的代码可读性。 3.不须要建立第三方来监听事件和传输数据。 4.一个控制器能够实现多个代理,知足自定义开发需求,可选必选有较大的灵活性。安全
缺点: 1.实现委托的代码过程比较繁琐。 2.当实现跨层传值监听的时候将加大代码的耦合性,而且程序的层次结构将变的混乱。 3.当对多个对象同时传值响应的时候,委托的易用性将大大下降。多线程
2.NotificationCenter(通知) 通知也是iOS开发中经常使用的一种传值响应方法,例如在AVFoundation框架中的MPMoviePlayerController在监听播放状态改变,播放中止等事件时使用的也正是NotificationCenter。官方给出的解释以下:闭包
An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table.架构
Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling these methods several times.app
Each running Cocoa program has a default notification center. You typically don’t create your own. An NSNotificationCenter object can deliver notifications only within a single program. If you want to post a notification to other processes or receive notifications from other processes, use an instance of NSDistributedNotificationCenter.框架
NSNotification采用的单例设计模式,当给通知中心注册一个key之后,那么不管在什么地方只要给通知中心发送一个这个key的消息,那么就实现了通讯传值,注册的通知的对象就会调用相应的方法。async
优势: 1.使用简单,代码精简。 2.解决了同时向多个对象监听相应的问题。 3.传值方便快捷,Context自身携带相应的内容。 缺点: 1.使用完毕后,要时刻记得注销通知,不然将出现不可预见的crash。 2.key不够安全,编译器不会监测是否被通知中心正确处理。 3.调试的时候动做的跟踪将很难进行。 4.当使用者向通知中心发送通知的时候,并不能得到任何反馈信息。 5.须要一个第三方的对象来作监听者与被监听者的中介。
3.Block(代码块) Block是iOS4.0+ 和Mac OS X 10.6+ 引进的对C语言的扩展,用来实现匿名函数的特性。苹果官方对于Block的解释也是十分的简洁:
A block is an anonymous inline collection of code, and sometimes also called a “closure”.Blocks are a powerful C-language feature that is part of Cocoa application development. They are similar to “closures” and “lambdas” you may find in scripting and programming languages such as Ruby, Python, and Lisp. Although the syntax and storage details of blocks might at first glance seem cryptic, you’ll find that it’s actually quite easy to incorporate blocks into your projects’ code.
翻译过来就是,闭包是一个可以访问其余函数内部变量的函数。Block的使用在这里也不在赘述(不知道的同窗能够点这个连接,我以为写的很详细)。不管是原生的框架仍是第三方的框架,咱们均可以看到Block的身影,Block强大的功能性和易用性使得深受架构师的喜好。
官方也有一篇简短的指导文档,有兴趣的同窗能够看一下(官方指导)。值得注意的是,在这份文档中也提到了几种Block的使用场合。
优势: 1.语法简洁,实现回调不须要显示的调用方法,代码更为紧凑。 2.加强代码的可读性和可维护性。 3.配合GCD优秀的解决多线程问题。
缺点: 1.Block中得代码将自动进行一次retain操做,容易形成内存泄露。 2.Block内默认引用为强引用,容易形成循环引用。
1.代理
2.通知
3.Block
1.回调方法 在平常的开发过程当中,咱们常常会遇到一些完成以后的处理问题,好比完成网路请求以后的回调,或者页面加载完成以后的回调等。这个时候咱们通常使用的是前二者方法,即Block或者Delegate。而在一对一传输回 调的时候明显Block的使用更加的简单高效,只须要在代码块中执行所须要的操做便可。在一对多的状况下,Delegate更加可以发挥出本身的优点。
2.跨层通讯 有的时候咱们须要实如今两个毫无关联的对象之间的通讯,这个时候若是使用Block或者Delegate就势必会增长代码的耦合性,这样对于代码的结构来讲是不健康的,所以这个时候使用Notification即是明智的选择。
3.UI响应事件 用户在于App的UI进行互动的时候,总会须要App进行交互响应,这个时候就毫无疑问的使用代理设计模式。而苹果官方给出的建议也是能够确定的,在Cocoa Touch框架中咱们也能够在几乎全部的UI交互控件的头文件里看到Delegate的成员变量,也正是印证了在UI响应事件上Delegate有着绝对的优点。
4.简单值得传递 当须要进行简单值得传递的时候,好比子控件传输给父控件所点击的IndexPath的时候,更加适合使用Block来传值。由于,若是只是为了传这一个简单的值而没有特别的业务处理而定义一个协议,而后实现协议,设置代理再写方法的话将十分麻烦,得不偿失,这个时候简单高效的Block就能够完美的替代Delegate完成任务了。