iOS开发Delegate,Notification,Block使用体会

本文探讨了iOS开发中常见的三种通信方式:Delegate、NotificationCenter和Block。介绍了它们的基本概念、优缺点及使用场景,强调了Delegate在UI响应事件上的优势,Notification在跨层通信中的便利性,以及Block在简单值传递和回调处理中的高效性。
摘要由CSDN通过智能技术生成

iOS开发Delegate,Notification,Block使用心得

(一)简要介绍

1.Delegate(代理、委托)

代理几乎是iOS开发中最常用的传值方式,在项目中的AppDelegate就是使用的这种设计模式,不仅如此,还有很多原生的控件也使用的这种设计模式,比如:UITextFiled,UITableView等等。官方给出的解释如下:

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.

具体使用方法不再示例(网上关于Delegate基础使用的博文数不胜数示例),主要总结一下Delegate的优点和缺陷。

优点: 

1.减少代码的耦合性,使事件监听和事件处理相分离。 

2.清晰的语法定义,减少维护成本,较强的代码可读性。 

3.不需要创建第三方来监听事件和传输数据。 

4.一个控制器可以实现多个代理,满足自定义开发需求,可选必选有较大的灵活性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值