OC——通知(NSNotificationCenter)基本声明方法
来源:互联网 发布:剑网三捏脸数据百度云 编辑:程序博客网 时间:2024/06/05 15:12
//NSNotificationCenter //使用步骤 //1、注册通知 //2、关联监听到通知之后触发的方法 //3、发送通知 //4、触发方法执行,处理相应逻辑 //5、注册对象销毁时(dealloc),移除通知(在哪个文件注册的通知就应该在哪个文件移除观察者)//.h文件//// Bus.h// 8.03_1//// Created by rimi1 on 16/8/3.// Copyright © 2016年 Wangxingqian. All rights reserved.//#import <Foundation/Foundation.h>@interface Bus : NSObject@property (nonatomic, strong) NSString *name;@property (nonatomic, strong) NSString *code;- (instancetype)initWithName:(NSString *)name code:(NSString *)code;@end//.m文件//// Bus.m// 8.03_1//// Created by rimi1 on 16/8/3.// Copyright © 2016年 Wangxingqian. All rights reserved.//#import "Bus.h"@implementation Bus- (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self];}- (instancetype)init{ self = [super init]; if (self) { _name = @"煞笔"; //1 //注册通知 addObserver:(观察者) selector:(接受到通知自动调用) name:@"name" object:nil [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nameChange) name:@"name" object:nil]; } return self;}- (instancetype)initWithName:(NSString *)name code:(NSString *)code { if (self = [super init]) { _name = name; _code = code; } return self;}- (void)setName:(NSString *)name { NSLog(@"路人乙以前的名字:%@,现在的名字:%@", _name, name); _name = name; //2 //发送通知(这里是当在 main 函数里面调用 name 的 set 方法时,会发送通知) [[NSNotificationCenter defaultCenter] postNotificationName:@"name" object:nil userInfo:nil]; }//3//接受通知时调用的方法- (void)nameChange { _code = @"123456"; NSLog(@"路人甲:智障,那我也帮你改身份证了。 身份证:%@", _code); }@end//main函数// main.m// 8.03_1//// Created by rimi1 on 16/8/3.// Copyright © 2016年 Wangxingqian. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"#import "Teacher.h"#import "Bus.h"int main(int argc, const char * argv[]) { @autoreleasepool { Bus *bus = [[Bus alloc] init]; bus.name = @"智障"; } return 0;}
0 0
- OC——通知(NSNotificationCenter)基本声明方法
- 通知基本用法NSNotificationCenter
- ios开发——NSNotificationCenter (通知中心)
- OC学习篇之---通知(NSNotificationCenter)
- OC传值之-通知,NSNotificationCenter
- OC——Block 声明方法
- NSNotificationCenter通知基本用法(一)
- NSNotificationCenter通知基本用法(二)
- IOS学习之——通知中心NSNotificationCenter
- iOS基础——通知代理之NSNotificationCenter、Delegate
- NSNotificationCenter 通知
- 通知NSNotificationCenter
- 通知--NSNotificationCenter
- 通知NSNotificationCenter
- NSNotificationCenter通知
- NSNotificationCenter没有处理好带来的通知方法执行多次
- 使用NSNotificationCenter 事件通知
- 通知中心NSNotificationCenter
- nginx 本地nginx.conf配置文件
- android开发过程中解决bug的方法
- 主线程中也不绝对安全的 UI 操作
- 使用Python进行简单的验证码识别
- 局域网文件与打印机共享
- OC——通知(NSNotificationCenter)基本声明方法
- 迭代器 iterator
- 侧拉新闻菜单
- 压缩感知稀疏基之离散小波变换
- HTML5新特性笔记
- Docker常见命令---简易教程
- 解释:i=i++
- android下如何限制安装APP的数量
- Eclipse下配置javah.exe自动生成jni头文件