//
// 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(@"我被点击了");
}
// 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(@"我被点击了");
}