iOS 开发

通知中心(NSNotificationCenter)

2016-10-08  本文已影响26人  G_M

由于项目的需要,最近也是在网上查看各种有关于通知中心的资料学习,但是都不是很全,今天就我个人而言对通知中心做一个总结,希望对大家有用.

屏幕快照 2016-10-08 20.13.50.png
#import "ViewController.h"

@interface ViewController ()

@end
@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];
NSNotification *note = [NSNotification notificationWithName:@"乔丹复出了" object:self userInfo:@{@"在那个队" : @"黄蜂队", @"工资是多少" : @"3000万"}];// 这句代码的意思是说:self像通知中心发送了一跳通知,通知的名称是"乔丹复出了",通知的内容是:userInfo这个字典里面的东西
// 利用通知中心,把通知发送到通知中心上
  [[NSNotificationCenter defaultCenter] postNotification:note];
}

2.再创建一个TestViewController

第一个参数observer:监听器,即谁要接收这个通知  第二个参数aSelector:收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入(即收到通知后要干什么事情)  第三个参数Name:通知的名称。如果为nil,那么无论通知的名称是什么,监听器都能收到这个通知   第四个参数Object:通知发布者。如果为Object和aName都为nil,监听器都收到所有的通知```

import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController

}

}

- 这样在TestViewController这个控制器里面就可以监听ViewController控制器里的信息.

上一篇 下一篇

猜你喜欢

热点阅读