Iphone 委托和通知之间有什么区别?

Iphone 委托和通知之间有什么区别?,iphone,objective-c,cocoa,cocoa-touch,xcode,Iphone,Objective C,Cocoa,Cocoa Touch,Xcode,委托和通知之间有什么区别 我像代表和礼节一样理解 @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 cl

委托和通知之间有什么区别

我像代表和礼节一样理解

@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];

    }

}
使用“id”的原因是什么?它们之间的区别是什么

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

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

像这样

classB{

-(void)DelegateMethod;

}
然后把这个叫进来

classA{

       classB delegate;

       -(void)viewdidload{

            [self.delegate DelegateMethod];

        }

    }

因此,从上面的中,我们避免了协议和id变量

但我知道我们很多人使用委托和协议。在这里,我了解了使用委托和协议的优点

下面是DelegateMethod函数的方法的协议实现的用法

而不是实例定义

@protocol使用的是什么。请任何一个人指引我正确的方向

我是iphone开发的新手。现在,我知道如何创建委托。但是当我来研究
NSNotification


这也是像委派一样做几乎正确的工作。因此,何时应该使用
delgate
NSnotification

代理使用协议并在两个类之间创建
has-A
关系。委托的另一个好处是,您可以将某些内容返回给所属类

另一方面,通知更倾向于点对多点通信。使用
NSNotification
的一个示例可能是在选项卡栏控制器应用程序中,在该应用程序中,您可能需要通知多个视图控制器某个特定事件,以便它们可以刷新数据等。这对于互不了解的类来说是非常好的,如果它们知道,则没有意义

现在,关于你的其他问题:

我们为什么使用
id

在类中,您需要一个类型不确定的对象的句柄,但它实现了您定义的协议。以
UIWebView
为例。可以有无限小类型的类作为它的委托,因此,它不应该命名特定类型的类,而是指定该类必须实现
UIWebViewDelegate
协议。这将耦合降低到绝对最小,并创建了一个高度内聚的应用程序,在该应用程序中,您将基于行为而不是状态创建交互

让我们来看一个例子:

@protocol ClassADelegate
- (NSString*) determineValue;
@end

@interface ClassA : NSObject
{
    id<ClassADelegate> delegate;
}
//  Make sure you are using assign, not retain or copy
@property (nonatomic, assign) id<ClassADelegate> delegate;

@end
在标题中,我们声明该类将实现
ClassADelegate
协议:

#import "ClassA.h"

@interface ClassB : NSObject <ClassADelegate>
{
}

- (void) someMethod;

@end

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

长答案:

委托通常是处理事情的一种更合适的方式,特别是当您正在创建一个供其他人使用的框架时。当您对委托使用协议时,您可以在编译时检查所需的方法,以便在编译时知道是否缺少任何所需的方法。对于NSNotificationCenter,您没有这样的保证

NSNotificationCenter有点“黑客式”,经常被新手程序员使用,这会导致糟糕的体系结构。很多时候,这两个功能是可以互换的,但更多的“硬核”开发人员可能会嘲笑NSNotificationCenter的使用


Q:使用“id”的原因是什么?它们之间的区别是什么

A:使用
id
可以将任何对象作为参数发送到方法。请注意,不能发送基本体,例如布尔、浮点、double、int等,除非它们被包装在各自的对象包装器中


然后把这个叫进来

classA{

   classB delegate;

   -(void)viewdidload{

        [self.delegate DelegateMethod];

    }

}

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

我们可以出于各种原因使用通知。例如,您可以广播通知,以更改用户界面元素基于程序中其他位置的特定事件显示信息的方式。或者,您可以使用通知来确保文档中的对象在文档窗口关闭之前保存其状态

通知的一般目的是通知程序事件的其他对象,以便它们能够做出适当的响应。但接收通知的对象只能在事件发生后做出反应。这与授权有很大区别

委托人有机会拒绝或修改委托对象提出的操作。观察物体,在o
#import "ClassA.h"

@interface ClassB : NSObject <ClassADelegate>
{
}

- (void) someMethod;

@end
#import "ClassB.h"
@implementation ClassB

- (void) someMethod
{
    ClassA* aClass = [[ClassA alloc] init];

    aClass.delegate = self;

   // Other work and memory clean up of A.
   // Some logic occurs in A where it calls the delegate (self) which will 
   // call the `determineValue` method of this class.
}

//  Here's the delegate method we implement
- (NSString*) determineValue
{
    return @"I did some work!";
}

@end
classB{

-(void)DelegateMethod;

}
classA{

   classB delegate;

   -(void)viewdidload{

        [self.delegate DelegateMethod];

    }

}
@protocol DelegateName
@required
- (void)method:(NSString *)param;
@optional
- (void)methodOptional:(NSString *)param;
@end
@property id <DelegateName> delegate;
myObject.delegate = <# some object conforming to DelegateName #>;
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(notificationHappened:)
                                             name:MyCustomNotificationName
                                           object:nil];
- (void)notificationHappened:(NSNotification *)notification {
    // do work here
}
[[NSNotificationCenter defaultCenter] postNotificationName:MyCustomNotificationName
                                                    object:self
                                                  userInfo:nil];