NSNotificationCenter用法总结

来源:互联网 发布:windows whistler.img 编辑:程序博客网 时间:2024/06/05 02:01

Notificationcenter的用法:

NSNotificationCenter未必一定要建在消息接收者的类中。可以放在别的类中,先实例化一下,然后把observer赋值为刚对象。

这里的observer相当于接受者(receiver),object相当于发送者(poster)。理解了这点就可以较灵活地使用通知了。


iPhone软件开发的时候会遇到这种情况:打开APP后会在后台运行某个方法,例如下载文件,下载完成后可能需要调用某个方法来刷新界面,这时候可能没法在下载的函数中回调。NSNotificationCenter(通知)是一个很好的选择。

通知使用起来灰常的简单:

1、定义通知:

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(callBack)     name: @"back"    object: nil];

2、定义通知中使用的方法:

- (void)callBack{NSLog(@"i am back.");}

3、调用通知:

- (void)getIT{NSLog(@"get it.");//发出通知[[NSNotificationCenter defaultCenter] postNotificationName:@"back" object:self];}

 

notificationcenter的讲解

Notifications

Notification 包装了事件的信息, 比如窗口正在获取焦点或者网络连接正在断开. 需要订阅事件(例如, 一个文件想要知道正在编辑它的窗口将要被关闭)的object需要在notification center注册, 之后当事件发生的时候就会得到通知. 当事件发生的时候, 一个notification会被发送到notification center, 而后notification center马上就会把这个notification转发给所有订阅过这个事件的object. 当需要的时候, notification会被缓存在notification queue中….

Notification的原理

在 两个object之间传递信息最标准的方法是传递消息 – 一个object调用另外一个object的方法. 但是, 传递的消息这种方法要求发送消息的object知道消息的接收者和它能接收的消息类型. 这将会把两个object紧密的绑定起来 – 最值得注意的是这会让两个本来独立的子系统耦合在一起. 为了解决这些问题, 广播模型被提了出来. Object只是负责发送notification, 而NSNotificationCenter将负责把这个notification转发给所有相关的object.

一个 NSNotification(在这片文章里面简称notification)包含一个name, 一个object和一个可选的dictionary. Name是notification的标识. Object包含notification发送者想要发送的任意类型的object(一般来说就是发送这个notification的object本 身). Dictionary用来保存其他相关的东西(如果有的话).

任意object都可以发送notification. 任意object都可以在notification center注册以便在某个事件发生的时候能够得到通知. Notification center负责把接受的notification发送给所有注册过的消息接收者. 发送notification的object, notification里面包含的object和接收这个notification的object可以是同一个object, 也可以是三个不同的object. 发送notification的object不需要知道关于接受者的任何信息. 但是, 接受者至少需要知道notification的name和其所包含dictionary的key(如果有的话).


Notification和Delegation

就使用上看, notification系统和delegate很像, 但是他们有以下不同:

*Notification的接受者可以是多个object. 但是delegate object只能有一个. 这就阻止了返回值.
*一个object可以从notification center接受它想要的任意数量个notification, 而delegate只能接受预先定义好的delegate方法.
*发送notification的object完全不知到接受者是否存在.


Notification Centers

Notification center负责接收和发送notification. 当它接受到notification的时候会通知所有符合特定条件的接受者. Notification信息被包装在NSNotification里. Notification接收者在notification center里面注册以获得其他object发出的notification. 当事件发生的时候, 一个object发送相关的notification到notification center. Notification center将消息分发给每一个注册过的接受者. 发送notification的object和接受者可能是同一个.


Cocoa包含两种notification center:

*NSNotificationCenter类管理单个进程内部的notification.
*NSDistributedNotificationCenter管理一台机器上跨进程的notification.


NSNotificationCenter

每 一个进程都有一个默认的notification center, 你可以通过访问 NSNotificationCenter 的 +defaultCenter方法来得到它. 这种类型的notification center负责管理单个进程内部的notification. 如果需要管理同一台机器上不同进程之间的notification则需要用到NSDistributedNotificationCenter.

Notification center发送notification给接收者的方法是同步的. 也就是说, 当发送一个notification的时候, 除非所有的接收者都接到和处理了这个notification, 否则不会返回. 想要发送异步notification的话就需要用到notification queue了.

在一个多线程应用程序里, notification总是和发送者处于同一个线程里, 但是接受者可以在其他线程里.


NSDistributedNotificationCenter

每 一个进程都有一个默认的distributed notification center, 你可以通过访问 NSDistributedNotificationCenter 的 +defaultCenter方法来得到它. 这种类型的notification center负责管理一台机器上多个进程之间的notification. 如果需要在多台机器间通讯的话, 使用distributed objects.

发送一个distributed notification是非常昂贵的. Notification首先会被发送到一个系统级别的服务器上, 然后在分别分发到每一个注册过的进程里. 从发从消息到消息被接受到之间的延迟理论上来说是无限的. 事实上, 如果太多的notification被发送到服务器上, 那么服务器上的notification队列可能会被撑满, 这就有可能会造成notification的丢失.

Distributed notification会在一个进程的主循环里被发送出去. 一个进程必须保证有一个主循环在其内部运行, 例如 NSDefaultRunLoopMode, 然后才能接受到distributed notification. 如果接收进程是多线程的, 那么notification并不一定会被主线程接受到. 一般来说notification会被分发到主线程的主循环, 但是其他线程一样可以接收到.

一 般类型的notification center可以注册所有object的notification, 但是 distributed notification center只能注册字符串类型的notification. 因为发送者和接受者可能在不同进程里, notification里面包含的object不能保证指向同一个object. 所以, distributed notification center只能接受包含字符串类型的notification. Notification会基于字符串来匹配.


NotificationCenter的使用

1. 定义一个方法

update

2.订阅通知

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(update) name:@"update" object:nil]

3. 在要发出通知消息的地方

[[NSNotificationCenter defaultCenter] postNotificationName:@"update" object:nil];

----------------------------

虚拟键盘显示和消失的通知

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWasShown:)

name:UIKeyboardDidShowNotification

object:nil];


[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWasHidden:)

name:UIKeyboardDidHideNotification

object:nil];

------

- (void)keyboardWasShown:(NSNotification *) aNotification

{

   if(keyboardShown)

         return;

   NSDictionary *info = [aNotification userInfo];//获取通知信息

   //get the size of the keyboard.

   NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];

   CGSize keyboardSize = [aValue CGRectValue].size;

   //Resize the scroll view

   CGRect viewFrame = [scrollView frame];

   viewFrame.size.height -= keyboardSize.height;

   //Scroll the active text field into view

   CGRect textFieldRect = [activeField frame];

   [scrollView scrollRectToVisible:textFieldRect animated:YES];

   keyboardShown = YES;

}

//Called when the UIKeyboardDidHideNotification is sent

- (void)keyboardWasHidden:(NSNotification *) aNotification

{

NSDictionary *info = [aNotification userInfo];

//Get the size of the keyboard.

NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

CGSize keyboardSize = [aValue CGRectValue].size;

//Reset the height of the scroll view to its original value

CGRect viewFrame = [scrollView Frame];

viewFrame.size.height += keyboardSize.height;

scrollView.frame = viewFrame;

keyboardShown = NO;

}


一般使用NSNotificationCenter的类方法defaultCenter就获得默认的通知对象,  也可以创建一个通知对象:

使用notificationWithName:object: 或者 notificationWithName:object:userInfo:建通知对象:

    NSNotification* notification = [NSNotification notificationWithName:kImageNotificationLoadFailed(connection.imageURL)
                                                                 object:self
                                                               userInfo:[NSDictionary dictionaryWithObjectsAndKeys:error,@"error",connection.imageURL,@"imageURL",nil]];


postNotificationName:object:userInfo:

- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo
Parameters
notificationName

The name of the notification.

notificationSender

The object posting the notification.

userInfo

Information about the the notification. May be nil.

从以上可以看出,object表示发送者!!而不是要发送的参数。要发送的参数可以放到information里面,是NSDictionary类型的

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 脚不小心扭伤了怎么办 娱乐之太帅了怎么办txt 走路走多了脚痛怎么办 胃疼怎么办简单按摩法 经常胃疼的厉害怎么办 半夜胃疼的厉害怎么办 吃消炎药伤胃了怎么办 吃药伤胃了胃疼怎么办 宝宝吃药伤胃了怎么办 胃胀胃痛怎么办快速解决方法 半夜2点3点胃疼怎么办 晚上吃多了胃疼怎么办 骨折打石膏后痒怎么办 脚脖子崴了肿了怎么办 喝酒喝的吐血了怎么办 感冒后咳嗽有痰怎么办 嗓子里老是有痰怎么办 物业把水停了怎么办 机洗衬衫缩水了怎么办 羊绒大衣洗缩水了怎么办 棉质衣服缩水了怎么办 衣服洗了变小了怎么办 毛衣洗后缩水了怎么办 鼻子又大又塌怎么办 苹果6被停用了怎么办 苹果6s手机停用怎么办 苹果4手机已停用怎么办 苹果手机5停用了怎么办 老公被骗300多万怎么办 苹果手机被抹除怎么办 钓鱼邮件点开了怎么办 幼犬吃多了拉稀怎么办 幼犬半夜醒了叫怎么办 相爱相杀的感情怎么办 冬天玩电脑手冷怎么办 被陌生人骗了钱怎么办 被网上骗了钱怎么办 20岁欠了10万怎么办 我赌博欠了10万怎么办 孩子见到生人不爱说话怎么办 18岁了个子矮小怎么办