简单介绍
通知:NSNotification,它的实质是程序内部提供的一种广播机制。把接受到的消息根据内部消息转发表,将消息转发给需要的对象。常用来页面间的传值。
实现通知的步骤:创建通知—>发布通知—>接收通知—>执行接收通知后调用的方法
NSNotification
通知类,由这个类创建的对象是一个通知对象。
这个类中有三个属性
//消息对象的唯一标识,接受通知消息时用来辨别,可以理解为通知的名字
@property (readonly, copy) NSNotificationName name;
//通知的发布者
@property (nullable, readonly, retain) id object;
//一个字典,用来传值
@property (nullable, readonly, copy) NSDictionary *userInfo;
- 创建通知
//第一种
//object:通知的发布者
//notificationWithName:通知的名字
//类方法创建一个通知对象,这个方法没有userInfo这个属性的初始赋值,用来发送无传值的通知
NSNotification *noti1 = [NSNotification notificationWithName:@"send" object:self];
//第二种
NSNotification *noti2 = [NSNotification notificationWithName:@"send" object:self userInfo:@{@"name":_registerView.nameTextfield.text,@"pass":_registerView.passTextField.text}];
NSNotificationCenter
通知中心类,通知中心是一个单例,每一个应用程序都有一个通知中心NSNotificationCenter实例, 专门负责协助不同对象之间的消息通信。
- 发布通知
// 发送通知,参数是一个通知对象
- (void)postNotification:(NSNotification *)notification;
// 发送通知,参数是通知的名称、发布者
- (void)postNotificationName: (NSNotificationName)aName object:(nullableid)anObject;
// 发送通知,参数是通知的名称、发布者、传递的参数
- (void)postNotificationName: (NSNotificationName)aName object:(nullableid)anObject userInfo:(nullableNSDictionary *)aUserInfo;
//三个方法虽然写法不同,但是功能一样
//后两种方法是初始化通知并发送通知,将通知对象的初始化和发送方法结合。
- 注册通知
//要想能够接收到通知,就得提前注册通知到通知中心,否则不会接收到。
//添加观察者,可以指定一个方法、名称和对象,接受到通知时执行这个指定的方法。
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullableNSNotificationName)aName object:(nullableid)anObject;
- 移除通知
//移除所有通知
- (void)removeObserver:(id)observer;
//根据name移除指定通知
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
实现步骤
下面用登录注册界面举例
这里省略掉登录、注册页面中的view.h view.m
登录页面中有: loginButton,registerButton,nameTextfield,passTextField;
注册页面中有:confirmButton,nameTextfield,passTextField
//RegisterController.h
#import <UIKit/UIKit.h>
#import "RegisterView.h"
@interface RegisterController : UIViewController
@property RegisterView *registerView;
@end
//RegisterController.m中
#import "RegisterController.h"
@interface RegisterController ()
@end
@implementation RegisterController
- (void)viewDidLoad {
[super viewDidLoad];
_registerView = [[RegisterView alloc] initWithFrame:self.view.frame];
[self.view addSubview:_registerView];
[_registerView initView];
[_registerView.confirmButton addTarget:self action:@selector(pressBack) forControlEvents:UIControlEventTouchUpInside];
}
- (void)pressBack {
//1.创建通知
NSNotification *noti = [NSNotification notificationWithName:@"send" object:self userInfo:@{@"name":_registerView.nameTextfield.text,@"pass":_registerView.passTextField.text}];
//2.通知中心去发布通知
[[NSNotificationCenter defaultCenter] postNotification:noti];
//通知对象的初始化和发送方法结合
//[[NSNotificationCenter defaultCenter] postNotificationName:@"send" object:self userInfo:@{@"name":_registerView.nameTextfield.text,@"pass":_registerView.passTextField.text}];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
//LoginController.m中
#import <UIKit/UIKit.h>
#import "LoginView.h"
NS_ASSUME_NONNULL_BEGIN
@interface Controller : UIViewController
@property LoginView *loginView;
@end
//LoginController.h中
#import "LoginController.h"
#import "RegisterController.h"
#import "LoginView.h"
@interface Controller ()
@end
@implementation Controller
- (void)viewDidLoad {
[super viewDidLoad];
_loginView = [[LoginView alloc] initWithFrame:self.view.frame];
[self.view addSubview:_loginView];
[_loginView initView];
[_loginView.registerButton addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];
}
- (void)pressRegister {
RegisterController * registerController = [[RegisterController alloc] init];
[self presentViewController:registerController animated:YES completion:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//3.注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recive:) name:@"send" object:nil];
}
//4.接收通知后调用的方法
- (void)recive:(NSNotification *)noti {
NSDictionary *dic = noti.userInfo;
_loginView.nameTextfield.text = dic[@"name"];
_loginView.passTextField.text = dic[@"pass"];
}
-(void)dealloc{
//移除观察者
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end