委托和通知有什么区别? [英] What is the difference between Delegate and Notification?

查看:25
本文介绍了委托和通知有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

委托和通知有什么区别?

What is the difference between Delegate and Notification?

我理解为委托和协议,

@protocol classADelegate

-(void)DelegateMethod;


@end



classB <classADelegate>{

   classA *ObjOfclassA=[[classA alloc]init];

    ObjOfclassA.delegate=self;

//while push later, here we have taken the pointer of classB(self) to `classA` and stored in delegate variable of classA. So, from `classA` we can call the function in `classB`

   push:classA from here.


   -(void)DelegateMethod{

        nslog(@"i am rithik from India");


     }

}


classA{

   id <classADelegate> delegate;

   -(void)viewdidload{

        [self.delegate DelegateMethod];

    }

}

我的疑问是

1. 为什么我们在 classA 中不用这样

1. Why don't we use in classA like this

classA{

**classB** <classADelegate> delegate;


[self.delegate DelegateMethod];

}

使用id"的原因是什么,它们有什么区别?

What is the reason for using "id" and what is the difference of them?

2.我们调用了classB的DelegateMethod函数的方法,该方法来自协议定义.

2. we have call the method of classB's DelegateMethod function which is came from protocol definition.

相反,我们可以通过定义 classB 的实例方法直接调用该方法.因为我们在 classA 的委托变量中获得了 classB 的指针.

instead we can straightly call that method by defining that instance method of classB.because we have the got the pointer for classB in classA's delegate variable.

像这样.

classB{

-(void)DelegateMethod;

}

然后在

classA{

       classB delegate;

       -(void)viewdidload{

            [self.delegate DelegateMethod];

        }

    }

<小时>

所以,从上面我们已经避免了协议和 id 变量.

但我知道我们很多人都使用委托和协议.到这里才知道使用delegate和protocol有什么好处

but I knew many of us use delegate and protocol. Here I came to know about any advantages while use delegate and protocol

这里是DelegateMethod函数的方法的协议实现的用法.

here what is the usage of protocol implementation for method of DelegateMethod function.

改为实例定义.

@protocol 的那些用法是什么.请任何人指导我正确的方向.

What is the usage of those by @protocol. Please any one guide me right direction.

我是 iphone 开发的新手.现在,我知道如何创建委托.但是当我来学习NSNotification

I'm new to iphone developement. Right now, I knew how to create delegate. but while I came to study about the NSNotification

这也是像委托一样做几乎正确的工作.那么,我什么时候应该使用 delgateNSnotification.

That is also do the almost right job like delegate. So, when should I use the delgate or NSnotification.

推荐答案

简短回答:您可以将代表视为电话.你打电话给你的朋友,特别想和他们谈谈.你可以说些什么,他们可以回应.你可以一直聊到挂断电话.委托以几乎相同的方式在两个对象之间创建链接,您不需要知道委托的类型,它只需要实现协议.另一方面,NSNotifications 就像一个广播电台.他们向愿意倾听的人广播他们的信息.广播电台无法收到听众的反馈(除非它有电话或代表).听众可以忽略该消息,或者他们可以对其进行处理.NSNotifications 允许您向任何对象发送消息,但它们之间没有链接来来回通信.如果您需要这种通信,您可能应该实现一个委托.否则,NSNotifications 更简单易用,但可能会给您带来麻烦.

Short Answer: You can think of delegates like a telephone call. You call up your buddy and specifically want to talk to them. You can say something, and they can respond. You can talk until you hang up the phone. Delegates, in much the same way, create a link between two objects, and you don't need to know what type the delegate will be, it simply has to implement the protocol. On the other hand, NSNotifications are like a radio station. They broadcast their message to whoever is willing to listen. The radio station can't receive feedback from it's listeners (unless it has a telephone, or delegate). The listeners can ignore the message, or they can do something with it. NSNotifications allow you to send a message to any objects, but you won't have a link between them to communicate back and forth. If you need this communication, you should probably implement a delegate. Otherwise, NSNotifications are simpler and easier to use, but may get you into trouble.

长答案:

委托通常是一种更合适的处理方式,尤其是在您创建供他人使用的框架时.当您将协议与委托一起使用时,您将获得编译时检查所需方法的时间,因此您可以在编译时知道是否缺少任何所需的方法.使用 NSNotificationCenter,您就没有这样的保证.

Delegates are usually a more appropriate way of handling things, especially if you're creating a framework for others to use. You gain compile time checking for required methods when you use protocols with your delegates, so you know when you compile if you're missing any required methods. With NSNotificationCenter you have no such guarantee.

NSNotificationCenter 是一种hack-ish",是经常使用的新手程序员,可能导致架构不佳.很多时候这两个功能是可以互换的,但更多的铁杆"开发者可能会对 NSNotificationCenter 的使用嗤之以鼻.

NSNotificationCenter is kind of "hack-ish" and is frequently used novice programmers which can lead to poor architecture. A lot of times these two features are interchangeable, but more "hardcore" developers might scoff at the use of the NSNotificationCenter.

问:使用id"的原因是什么,它们有什么区别?

答: 使用 id 允许您将任何对象作为参数发送到该方法.请注意,您不能发送诸如 bool、float、double、int 等原语,除非它们被包装在各自的 Object 包装器中.

A: Using id allows you to send any object to the method as a parameter. Note that you can not send primitives such as bools, floats, doubles, ints, etc. unless they are wrapped in their respective Object wrappers.

classB{

-(void)DelegateMethod;

}

然后调用这个

classA{

   classB delegate;

   -(void)viewdidload{

        [self.delegate DelegateMethod];

    }

}

您提供的上述示例要求 classA 的委托始终为 classB 类型,这并不有利.在这种情况下,您可能只使用引用其他类的变量,例如 myClassB,而不是使用委托.委托的美妙之处在于您可以传递任何对象,只要它们实现了所需的方法(编译器会确保,只要它被标记为正确的委托类型),代码就可以正常工作.

The above example you provided would require that classA's delegate always be of type classB, which isn't advantageous. Instead of using delegates in this scenario, you would probably just use a variable that referred to your other class, say, myClassB. The beauty of delegates is that you can pass around any object, and the code just works as long as they implement the required methods (which the compiler ensures, as long as it's marked as the correct delegate type).

这篇关于委托和通知有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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