编程笔记(Objective-c:委托与通知示例程序)

转载请标明出处:http://blog.csdn.net/zhangxingping
一个简单的委托与通知的示例程序。其中主程序如下:
#import <Foundation/Foundation.h>
#import "MyDelegate.h"
#import "MyNotificationObserver.h"
#import "MyClass.h"


NSString * notificationName = @"A notification."; //通知的名称

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    MyClass  *obj =[[MyClass alloc] init];
    MyDelegate * dlgt = [[ MyDelegate alloc] init];  //生成一个被委托者
    MyNotificationObserver * obsvr1 = [[MyNotificationObserver alloc] init]; //通知的观察者1
    MyNotificationObserver * obsvr2 = [[MyNotificationObserver alloc] init];//通知的观察者2
    
    //设置obj的委托对象
    obj.delegate = dlgt;
    //注册观察者1为obj对象发布的notificationName 通知的一个观察者
    [[NSNotificationCenter defaultCenter] addObserver:obsvr1 selector:@selector(Notified:) name:notificationName object:obj];     
    //注册观察者2为obj对象发布的notificationName 通知的一个观察者
    [[NSNotificationCenter defaultCenter] addObserver:obsvr2 selector:@selector(Notified:) name:notificationName object:obj];
    
    //CallDelegateMethod方法中会调用其委托的方法
    [obj CallDelegateMethod];
    //通过缺省的通知中心,obj对象发布notificationName通知
    NSNotification * notif = [ NSNotification notificationWithName:notificationName object:obj];
    [[NSNotificationCenter defaultCenter] postNotification:notif];
    
    [obsvr1 release];
    [obsvr2 release];
    [dlgt release];
    [obj release];

    [pool drain];
    return 0;
}

上面程序中先生成一个MyDelegate类的对象,两个MyNotificationObserver类的对象;并分别将它们设置为obj对象的委托和消息的观察者。然后通过发送消息调用委托的方法;通过发布通知来触发观察者的指定方法Notified:。

 

其中MyClass类的声明和实现如下:

声明文件MyClass.h:

#import <Foundation/Foundation.h>

#import "MyDelegate.h"
#import "MyNotificationObserver.h"


@interface MyClass : NSObject 
{
@private
    
}

-(void) CallDelegateMethod; //该方法中会调用其被委托者的方法

@property(retain, nonatomic) MyDelegate * delegate; //该类的对象将有一个被委托者,将其作为属性。


@end


实现文件MyClass.m:

#import "MyClass.h"
#import "MyDelegate.h"
#import "MyNotificationObserver.h"


@implementation MyClass
@synthesize delegate;
- (id)init
{
    self = [super init];
    if (self)
    {
        // Initialization code here.
    }
    
    return self;
}

- (void)dealloc
{
    [self.delegate release]; //由于对其被委托者是retain的方式引用的,因此在销毁自身对象前,需要先释放对被委托者的引用
    [super dealloc];
}

-(void) CallDelegateMethod
{
    [self.delegate method]; //调用其被委托者的方法
}

@end


被委托者MyDelegate类的定义和实现如下:

声明文件MyDelegate.h

#import <Foundation/Foundation.h>


@interface MyDelegate : NSObject {
@private
    
}

-(void)method;//被委托者中的方法

@end

实现文件MyDelegate.m

#import "MyDelegate.h"


@implementation MyDelegate

- (id)init
{
    self = [super init];
    if (self) 
    {
        // Initialization code here.
    }
    
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

-(void) method
{
    NSLog(@"Delegate: %@",self);
}

@end


观察者的声明和实现文件如下:

声明文件MyNotificationObserver.h:

#import <Foundation/Foundation.h>


@interface MyNotificationObserver : NSObject {
@private
    
}

-(void)Notified:(id)sender;//观察到通知后将调用的方法
@end

实现文件如下MyNotificationObserver.m

#import "MyNotificationObserver.h"


@implementation MyNotificationObserver

- (id)init
{
    self = [super init];
    if (self) 
    {
        // Initialization code here.
    }
    
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

-(void)Notified:(id)sender
{
    NSLog(@"%@ is notified with sender = %@",self, sender);
}


@end

上述程序的运行结果如下:
Delegate: <MyDelegate: 0x10010c8d0>

<MyNotificationObserver: 0x10010ca30> is notified with sender = NSConcreteNotification 0x100110ca0 {name = A notification.; object = <MyClass: 0x10010c740>}

<MyNotificationObserver: 0x10010ca70> is notified with sender = NSConcreteNotification 0x100110ca0 {name = A notification.; object = <MyClass: 0x10010c740>}

在实际的开发过程中,被委托者的类往往是需要遵循一定的协议。这些协议中定义了被委托者需要实现的方法。委托者在适当的时候会向其被委托者法送适当的消息,从而调用被委托者的特定方法来完成对特定事务的处理。


从上面的示例程序中可以看出委托机制和通知机制有几分相似,他们的主要区别在于:

1. 通知机制中可有任意多的对象接收到某一个通知;而委托机制中的委托者通常只会把某些特定事务的处理委托给一个被委托者,而不是多个。这样以来,委托者可以通过返回值来关心被委托者对事务的处理情况;而通知的发布者则不能通过返回值关心到通知的观察者对通知的处理情况。

2. 一个观察者可以通过注册不同消息的方式来关注到多个来自通知中心的消息;而委托机制中的被委托者的方法往往是预先定义好的。

3. 通知机制中的消息的发布者是不感知观察者的存在的;而委托机制中的委托者是明确感知得到被委托者的存在的。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值