问题描述
在Monotouch中进行开发时,它对我们真正的.NET event
s还是NSNotificationCenter
来说更好"?
When developing in Monotouch, is it "better" to us real .NET event
s or NSNotificationCenter
?
一个简单的例子:我有一个UIViewController
.它提供了一个事件" CallbackWhenDisappeared ". ViewDidDisappear
中触发了此事件.有兴趣的人可以注册参加该活动.
Simple example: I have a UIViewController
. It offers an event "CallbackWhenDisappeared". This event is triggred in ViewDidDisappear
. Who ever is interested can register to the event.
我还可以在NSNotificationCenter
上发布" MyFancyControllerHasDisappeared ",然后让感兴趣的对象在此订阅.
I could as well post a "MyFancyControllerHasDisappeared" on the NSNotificationCenter
and let interested objects subscribe there.
哪个版本是首选?
我看到的.NET事件的缺点:消失的控制器可能持有对订阅控制器的引用(或者反过来?),并且可能不会被垃圾回收.
The disadvantage with the .NET events I see: the disappearing controller might hold a reference to the subscribing controller (or the other way round?) and might not be garbage collected.
与类确实必须彼此了解的事件相比,我也更喜欢使用NSNotificationCenter时的松散耦合.
I also like the loose coupling when using NSNotificationCenter compared to the events where the classes really have to know each other.
这样做有错误或正确的方法吗?
Is there a wrong or a right way of doing it?
推荐答案
没有真正的对与错,但在我看来,它看起来是这样的:
There is no really right or wrong, but in my opinion it looks so:
NotificationCenter-您不知道哪些对象对事件"感兴趣,您将其发送出去,任何对象都可以接收它
NotificationCenter - You don't know which Objects are interested on the "Events", you send it out and any object can receive it
.Net事件-如果两个对象之间存在直接连接,则可以使用此方法,例如,如UIViewController将另一个UIViewcontroller显示为Modal. ModalUIViewcontroller会触发一个事件,如果该事件将隐藏并且UIViewController被限制为该事件
.Net Events - If there is a direct connection between two objects use this, for example like an UIViewController shows an other UIViewcontroller as Modal. The ModalUIViewcontroller fires an event, if it will hide and the UIViewController is Suscribed to it
这篇关于使用Monotouch时,我应该使用NSNotificactionCenter还是.NET事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!