您的位置:首页 > 编程语言

验证通知是同步还是异步执行....据代码验证,通知其实是一个同步执行的过程,按照通知中心发布通知,然后订阅通知,接收通知step by step

2017-05-01 11:41 302 查看
//
//  ViewController.m
// 
演练通知是同步还是异步
//
//  Created by doublek on 2017/5/1.
//  Copyright © 2017年
doublek. All rights reserved.
//

#import
"ViewController.h"

@interface
ViewController ()

@end
//重用标识
static
NSString *kNotificationName =
@"NSNotificationName";
@implementation ViewController

- (void)viewDidLoad {

    [super
viewDidLoad];

   

    UIButton *button = [UIButton
buttonWithType:UIButtonTypeContactAdd];

   

    button.center =
self.view.center;

    [self.view
addSubview:button];

   

    [button addTarget:self
action:@selector(buttonclick)
forControlEvents:UIControlEventTouchUpInside];

   

    //注册通知

    [[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(actionNotification:)
name:kNotificationName
object:nil];

   

}

-(void)actionNotification:(NSNotification
*)notification{

   

    NSLog(@"发布通知");

    //睡眠三秒

    sleep(3);

   

    NSString *message = notification.object;

   

    NSLog(@"%@",message);

   

}

-(void)buttonClick{

   

    //接收通知

    [[NSNotificationCenter
defaultCenter]
postNotificationName:kNotificationName
object:@"通知已经被接收"];

   

    NSLog(@"我被点击了");

   

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐