iOS SDK中的通知中心与委派 [英] Notification Center vs. Delegation in iOS SDK

查看:67
本文介绍了iOS SDK中的通知中心与委派的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple为什么选择使用委托在SDK对象之间进行某些通信,并为其他对象将通知发布到Notification Center?

Why did Apple choose to use delegation for some communication amongst SDK objects and to post notifications to Notification Center for others?

特别是,我在考虑来自UIWindow的一组键盘外观通知。

In particular, I'm thinking about the set of keyboard appearance notifications that come from UIWindow.

是因为通知中心系统意味着多个对象可以使用键盘外观操作作为触发来更改其状态,而只有一个对象可以

Is it because the Notification Center system means that more than one object can use the keyboard appearance action as a trigger to change its state, whereas only one object would have been able to act with a delegate implementation?

推荐答案

通过委托,您可以向后执行方法(并可选地传递参数)。例如,如果您有一个班级引用了另一个班级(父级和子级),则子级可以在不引用父级的情况下退回到父级。

Delegation allows you to execute methods (and optionally pass parameters) "backward." For example, if you have one class that references another class (parent and child), the child can fire back to the parent without referencing the parent.

通知中心,位于另一方面,聆听并等待直到听到所等待的消息。这样,多个视图控制器中的多个侦听器可以等待并收听给定的消息。

Notification Center, on the other hand, listens and waits until it hears the message it is waiting for. This allows for multiple listeners in multiple view controllers to wait and listen for a given message.

对于关系,委派是一对一关系,而通知中心是1。到1 /许多关系。

As for relations, delegation is a 1 to 1 relationship while Notification Center is a 1 to 1/many relationship.

这篇关于iOS SDK中的通知中心与委派的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆