iOS开发之设计模式(二)

cocoa框架中很多地方都用到了观察者模式,可见观察者模式也是一种很重要的设计模式。观察者模式包含两种机制:KVO机制通知机制

观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己或者做出相应的一些动作。在之前的文章已经介绍过了KVO机制,本文重点讲解通知机制。

对通知机制的观察者模式可理解为下图:

下面通过一个例子来了解通知机制:点击HXRedView上的按钮redView,就向通知中心注册通知,并让通知中心广播该通知。然后在合适的位置注册监听,来监听该通知,并做出相应的反应

HXRedView.h

#import <UIKit/UIKit.h>
@interface HXRedView : UIView

@end

HXRedView.m

#import "HXRedView.h"

@interface HXRedView ()
@property (nonatomic, weak) UIButton *greenButton;

@end

@implementation HXRedView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor redColor];
        
        // 创建按钮
        UIButton *greenButton = [[UIButton alloc] init];
        [greenButton setTitle:@"绿色按钮" forState:UIControlStateNormal];
        [greenButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [greenButton setBackgroundColor:[UIColor greenColor]];
        
        [greenButton addTarget:self action:@selector(clickGreenButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:greenButton];
        self.greenButton = greenButton;
    }
    return self;
}

/**
 *  点击按钮的监听方法
 */
- (void)clickGreenButton {
    
    // 向通知中心注册通知
    NSNotification *noti = [NSNotification notificationWithName:@"KGreenButtonDidClicked" object:self];
    // 通知中心广播通知
    [[NSNotificationCenter defaultCenter] postNotification:noti];
}

/**
 *  布局子控件尺寸和位置
 */
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.greenButton.frame = CGRectMake(50, 50, 200, 80);
}

@end

这里我们在控制器view加载完毕后注册监听对象,来监听通知。

ViewController.m

#import "ViewController.h"
#import "HXRedView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // 创建HXRedView对象
    HXRedView *redView = [[HXRedView alloc] initWithFrame:CGRectMake(10, 64, 300, 200)];
    [self.view addSubview:redView];
    
    
    // 向通知中心注册通知监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptNoti) name:@"KGreenButtonDidClicked" object:redView];
}

- (void)acceptNoti {
    NSLog(@"%@收到了通知(绿色按钮被点击)", self);
}
- (void)dealloc {
    // 移除自身上的所有通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

运行结果:

打印结果:

使用通知机制的步骤:

1.    向通知中心注册通知

NSNotification*noti = [NSNotification notificationWithName:@"KGreenButtonDidClicked"object:self];

// aName:通知名

// anObject:哪个对象发出的通知

2.    通知中心广播通知

[[NSNotificationCenterdefaultCenter] postNotification:noti];

3.    对该通知有兴趣的对象,在合适的时候向通知中心注册监听对象

[[NSNotificationCenterdefaultCenter] addObserver:self selector:@selector(acceptNoti) name:@"KGreenButtonDidClicked"object:redView];

// observer:谁监听

// aSelector:监听到通知后的回调方法

// aName:要监听的通知名

// anObject:谁发出的通知

4.    实现监听回调方法

-(void)acceptNoti {

    NSLog(@"%@收到了通知(绿色按钮被点击)", self);

}
注意:一个对象向通知中心注册成为监听对象之后,在该对象销毁之前把该监听移除。

[[NSNotificationCenter defaultCenter] removeObserver:self];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值