七.(一)oc通知-NSNotification(通知中心-NSNotificationCenter)

6年了,我来补上

先来最简单的吧。复制粘贴即可用

!⚠️通知传值先有接收通知,再有发起通知。为什么呢?以前开发遇到的人教我的南增教的

1.接收通知部分

#import "VC1.h"
@interface VC1 ()

@end

@implementation VC1

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    // 注册通知,也是观察通知,接收通知消息和值
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotificationAction:) name:@"notificationName" object:nil];
}
- (void)getNotificationAction:(NSNotification*)note{
    
    NSDictionary *dict = note.userInfo;
    NSString *name = dict[@"name"];
    NSString *age = dict[@"age"];
    NSString *height = dict[@"height"];
    
    NSLog(@"接收到通知内容姓名:%@",name);
    NSLog(@"接收到通知内容年龄:%@", age);
    NSLog(@"接收到通知内容身高:%@", height);
}
@end

 

2.发起通知部分

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];
///发起通知带参数
    NSDictionary *dict = @{@"name":@"lnj", @"age":@"30", @"height":@"1.75"};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"notificationName" object:nil userInfo:dict];

}

@end

 

后面开始深入一些,有空再完善!

 

posted on 2016-12-06 09:21  高彰  阅读(272)  评论(0编辑  收藏  举报

导航