哪个更好 - 依赖委托还是通过对象持久化 - iOS
经过一番阅读后,我注意到苹果公司推动在控制器上使用委托来相互通信。
假设我有一个菜单控制器,需要与帮助控制器进行通信(并且它们需要共享信息 - 哈哈,不要问我为什么要共享信息)。我可以通过委托来完成此操作,但是当我可以通过帮助对象传递所需信息并让帮助控制器拾取在前一个控制器中更改的信息时,为什么要使用委托呢?
代表仍然是成对的——无论它有多松散,使用对象难道不是可重用性的最佳实践吗?
After doing some reading I notice Apple pushes the use of delegates on controllers communicating with one another.
So say I have a Menu controller which needs to communicate with the help controller (and they need to share info - lol don't ask me why they would). I could do this through delegates, but why use delegates when I can pass the required information on through a help object and have the help controller pick back up this info that was changed in the previous controller.
Delegates still couples - however loosely it may be, wouldn't the use of objects be the best practice here for re usability?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代表对于响应非常有用,尤其是在不会立即响应的情况下。
如果您只是告诉帮助控制器要显示哪些信息之类的信息,那么直接传递它就很好。例如,如果您想知道帮助控制器何时完成向用户显示帮助,则委托会更清晰且通常更优越。
Delegates are great for responses, especially if the response isn't going to be immediate.
If you're simply telling the help controller something like what information to display, passing it directly is great. If you want to know, for example, when the help controller is done displaying help for the user a delegate is clearer and usually superior.