美文网首页线程
iOS 通知Notification

iOS 通知Notification

作者: 行者无疆漫步云端 | 来源:发表于2016-05-12 22:14 被阅读89次

//  ViewController.m

//  通知的使用

//

//  Created by yunhuihuang on 16/5/12.

//  Copyright © 2016年 yhhuang. All rights reserved.

//

/*

通知实现

通知的同步,他的响应者之间同步

通知的异步如何实现,异步需要使用同步队列,在这个程序中是同步的

*/

#import "ViewController.h"

@interface ViewController (){

NSNotificationCenter *center;

NSNotification *notication1;

NSNotification *notication2;

NSNotificationQueue *queue;

NSNotificationQueue *queue1;

}

@end

@implementation ViewController

- (IBAction)btnNoticationOne:(id)sender {

[center postNotificationName:@"test1" object:nil];

NSLog(@"test1 finish");

}

- (IBAction)btnNoticationTwo:(id)sender {

[center postNotificationName:notication1 object:nil];

NSLog(@"test1 finish");

[center postNotificationName:notication2 object:nil];

NSLog(@"test2 and test1 finish");

// 将通知添加到队列中异步执行

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//初始化通知中心

center = [NSNotificationCenter defaultCenter];

//添加通知名字

notication1 = [NSNotification notificationWithName:@"test1" object:nil];

notication2 =[NSNotification notificationWithName:@"test2" object:nil];

//添加多个通知队列

queue = [NSNotificationQueue defaultQueue];

queue1 = [NSNotificationQueue defaultQueue];

[queue enqueueNotification:notication1 postingStyle:2];

[queue1 enqueueNotification:notication2 postingStyle:2];

//添加通知notication1的观察者

[center addObserver:self selector:@selector(second) name:notication1 object:nil];

[center addObserver:self selector:@selector(first) name:notication1 object:nil];

//添加通知notication2的观察者

[center addObserver:self selector:@selector(third) name:notication2 object:nil];

}

-(void)first{

NSLog(@"first");

}

-(void)second{

NSLog(@"second");

//sleep(30);

}

-(void)third{

NSLog(@"third");

}

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

[center removeObserver:self];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

相关文章

  • iOS适配

    1.iOS10的适配 (1).Notification(通知) 自从Notification被引入之后,苹果就不断...

  • 通知及Block传值代码示例

    通知 在IOS中,主要有广播通知(broadcast notification)、本地通知(local notif...

  • iOS 通知Notification

    // ViewController.m // 通知的使用 // // Created by yunhuihuang...

  • iOS通知(Notification)

  • iOS通知Notification

    通知模式:⼀一个对象能够给其他任意数量的对象⼲⼴广播信息。对象之 间可以没有耦合关系。 NSNotificatio...

  • iOS Notification(通知)

    通知机制想必大家都很熟悉,平常的开发中或多或少的应该都用过。它是 Cocoa 中一个非常重要的机制,能把一个事件发...

  • iOS 通知机制总结

    iOS中提供了2种推送通知 本地推送通知(Local Notification) 远程推送通知(Remote No...

  • iOS-消息推送

    iOS 消息推送包括远程推送通知(Remote Notification)和本地推送通知(Local Notifi...

  • 26 - 推送

    iOS中提供了2种推送通知: 本地推送通知(Local Notification) 远程推送通知(Remote N...

  • iOS 远程通知

    iOS推送通知分为两种: 远程推送通知(Remote Notification) 苹果的远程通知服务 APNs(A...

网友评论

    本文标题:iOS 通知Notification

    本文链接:https://www.haomeiwen.com/subject/cnewxttx.html