通知传值 一般用在 1.页面的关联性不大,但是需要传递信息的地方. 一个页面作为发送者 一个页面作为监听者
2.向多个页面发送信息的时候,可将一个页面作为发送者 其他的页面作为监听者通过通知名称获取通知 利用通知中的 userInfo中的数据 执行相应的操作
通知传值 :一般在要接收数据的页面都要建立 监听器 用于监听获取自己需要的数据
作为发送者 要发送通知 并指定通知名称 和 通知中的信息(以字典的形式保存) 并指定触发通知的时机
通知传值三步骤: 1.注册通知 2.发送通知 3.移除通知
下面这个例子中 有两个页面 通过 tabBarController 联系起来 第一个页面 作为通知的发送者和监听者 第二个页面作为通知的监听者
(这个代码 使用 tabBar 有一个问题 就是当第一次执行的时候 第二个页面没有反应 这是因为当第一次执行的时候 第一个页面发送通知 但是第二个页面还没有创建加载 所以接收不到通知)
//第一个页面
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITextViewDelegate]]]]>
@end
//1.
注册通知
//2. 发送通知
//3. 移除通知
#import "ViewController.h"
@interface ViewController ()
{
IBOutlet UITextView *_textView;
}
@end
@implementation ViewController
- ( void )viewDidLoad
{
[ super viewDidLoad ];
// Do any additional setup after loading the view, typically from a nib.
// 注册一个通知中心 注册了一个 anObserve ( 监听 ) 对象 : 接收名字为 notificationName 发送者为 anObject 的 notification . 当 anObject 发送名字为 notificationName 的 notification 时 将会调用 anObserver 的 aSelector 方法 参数为 notification
// 如果 notificationName 为 nil, 那么 notification center 将 anObject 发送的所有 notification 转发给 observer 如果 anObject 为 nil, 那么 notification center 将所有的名字为 notificationName 的 notification 转发给 observer
// 当前控制器作为监听者 监听所有名字为 ColorChange 的通知
[[ NSNotificationCenter defaultCenter ] addObserver : self selector : @selector (doChange:) name : @"ColorChange" object : nil ];
_textView . delegate = self ;
}
- ( void )didReceiveMemoryWarning
{
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
// 当前的控制器发送通知 这个控制器作为一个通知的监听者 和 通知的发送者
// 改变颜色成红色
- ( IBAction )changeColorToRed:( UIButton *)sender {
NSDictionary *dic = [[ NSDictionary alloc ] initWithObjectsAndKeys : @"redColor" , @"color" , nil ];
// 通过通知中心 发送一个通知 当前对象作为发送者
[[ NSNotificationCenter defaultCenter ] postNotificationName : @"ColorChange" object : self userInfo :dic];
}
// 改变颜色成蓝色
- ( IBAction )changeColorToBlue:( UIButton *)sender {
NSString *textViewText = _textView . text ;
NSDictionary *dic = [[ NSDictionary alloc ] initWithObjectsAndKeys : @"blueColor" , @"color" , textViewText , @"text" , nil ];
// 发送通知
[[ NSNotificationCenter defaultCenter ] postNotificationName : @"ColorChange" object : self userInfo : dic];
}
//接收到通知的时候进行调用
- ( void )doChange:( NSNotification *)notification
{
// 通过字典 接收所接收到的用户通知中携带的信息
NSDictionary *dic = [notification userInfo ];
NSString *colorStr = [dic objectForKey : @"color" ];
UIColor *color = nil ;
if ([colorStr isEqualToString : @"redColor" ]) {
color = [ UIColor redColor ];
}
if ([colorStr isEqualToString : @"blueColor" ]) {
color = [ UIColor blueColor ];
}
self . view . backgroundColor = color;
}
// 在 dealloc 中移除通知 注意:通知是一定要移除的
- ( void )dealloc
{
[[ NSNotificationCenter defaultCenter ] removeObserver : self ];
[ _textView release ];
[ super dealloc ];
}
- ( BOOL )textView:( UITextView *)textView shouldChangeTextInRange:( NSRange )range replacementText:( NSString *)text
{
if ([text isEqualToString : @"\n" ]) {
[textView resignFirstResponder ];
}
return YES ;
}
@end
//2. 发送通知
//3. 移除通知
#import "ViewController.h"
@interface ViewController ()
{
IBOutlet UITextView *_textView;
}
@end
@implementation ViewController
- ( void )viewDidLoad
{
[ super viewDidLoad ];
// Do any additional setup after loading the view, typically from a nib.
// 注册一个通知中心 注册了一个 anObserve ( 监听 ) 对象 : 接收名字为 notificationName 发送者为 anObject 的 notification . 当 anObject 发送名字为 notificationName 的 notification 时 将会调用 anObserver 的 aSelector 方法 参数为 notification
// 如果 notificationName 为 nil, 那么 notification center 将 anObject 发送的所有 notification 转发给 observer 如果 anObject 为 nil, 那么 notification center 将所有的名字为 notificationName 的 notification 转发给 observer
// 当前控制器作为监听者 监听所有名字为 ColorChange 的通知
[[ NSNotificationCenter defaultCenter ] addObserver : self selector : @selector (doChange:) name : @"ColorChange" object : nil ];
_textView . delegate = self ;
}
- ( void )didReceiveMemoryWarning
{
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
// 当前的控制器发送通知 这个控制器作为一个通知的监听者 和 通知的发送者
// 改变颜色成红色
- ( IBAction )changeColorToRed:( UIButton *)sender {
NSDictionary *dic = [[ NSDictionary alloc ] initWithObjectsAndKeys : @"redColor" , @"color" , nil ];
// 通过通知中心 发送一个通知 当前对象作为发送者
[[ NSNotificationCenter defaultCenter ] postNotificationName : @"ColorChange" object : self userInfo :dic];
}
// 改变颜色成蓝色
- ( IBAction )changeColorToBlue:( UIButton *)sender {
NSString *textViewText = _textView . text ;
NSDictionary *dic = [[ NSDictionary alloc ] initWithObjectsAndKeys : @"blueColor" , @"color" , textViewText , @"text" , nil ];
// 发送通知
[[ NSNotificationCenter defaultCenter ] postNotificationName : @"ColorChange" object : self userInfo : dic];
}
//接收到通知的时候进行调用
- ( void )doChange:( NSNotification *)notification
{
// 通过字典 接收所接收到的用户通知中携带的信息
NSDictionary *dic = [notification userInfo ];
NSString *colorStr = [dic objectForKey : @"color" ];
UIColor *color = nil ;
if ([colorStr isEqualToString : @"redColor" ]) {
color = [ UIColor redColor ];
}
if ([colorStr isEqualToString : @"blueColor" ]) {
color = [ UIColor blueColor ];
}
self . view . backgroundColor = color;
}
// 在 dealloc 中移除通知 注意:通知是一定要移除的
- ( void )dealloc
{
[[ NSNotificationCenter defaultCenter ] removeObserver : self ];
[ _textView release ];
[ super dealloc ];
}
- ( BOOL )textView:( UITextView *)textView shouldChangeTextInRange:( NSRange )range replacementText:( NSString *)text
{
if ([text isEqualToString : @"\n" ]) {
[textView resignFirstResponder ];
}
return YES ;
}
@end
//在第二个页面 也可以接收通知
#import
"DetailViewController.h"
@interface DetailViewController ()
{
IBOutlet UILabel *_text;
}
@end
@implementation DetailViewController
- ( id )initWithNibName:( NSString *)nibNameOrNil bundle:( NSBundle *)nibBundleOrNil
{
self = [ super initWithNibName :nibNameOrNil bundle :nibBundleOrNil];
if ( self ) {
// Custom initialization
}
return self ;
}
- ( void )viewWillAppear:( BOOL )animated
{
// 注册通知中心 监听通知事件
[[ NSNotificationCenter defaultCenter ] addObserver : self selector : @selector (doChange:) name : @"ColorChange" object : nil ];
}
- ( void )viewDidLoad
{
[ super viewDidLoad ];
// Do any additional setup after loading the view.
}
- ( void )doChange:( NSNotification *)notifmication
{
NSDictionary *dic = [notifmication userInfo ];
NSString *colorStr = [dic objectForKey : @"color" ];
UIColor *color = nil ;
if ([colorStr isEqualToString : @"redColor" ]) {
color = [ UIColor redColor ];
}
if ([colorStr isEqualToString : @"blueColor" ]) {
color = [ UIColor blueColor ];
}
NSString *text = [dic objectForKey : @"text" ];
_text . text = text;
self . view . backgroundColor = color;
}
- ( void )didReceiveMemoryWarning
{
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
- ( void )dealloc
{
// 移除通知
[[ NSNotificationCenter defaultCenter ] removeObserver : self ];
[ _text release ];
[ super dealloc ];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
@interface DetailViewController ()
{
IBOutlet UILabel *_text;
}
@end
@implementation DetailViewController
- ( id )initWithNibName:( NSString *)nibNameOrNil bundle:( NSBundle *)nibBundleOrNil
{
self = [ super initWithNibName :nibNameOrNil bundle :nibBundleOrNil];
if ( self ) {
// Custom initialization
}
return self ;
}
- ( void )viewWillAppear:( BOOL )animated
{
// 注册通知中心 监听通知事件
[[ NSNotificationCenter defaultCenter ] addObserver : self selector : @selector (doChange:) name : @"ColorChange" object : nil ];
}
- ( void )viewDidLoad
{
[ super viewDidLoad ];
// Do any additional setup after loading the view.
}
- ( void )doChange:( NSNotification *)notifmication
{
NSDictionary *dic = [notifmication userInfo ];
NSString *colorStr = [dic objectForKey : @"color" ];
UIColor *color = nil ;
if ([colorStr isEqualToString : @"redColor" ]) {
color = [ UIColor redColor ];
}
if ([colorStr isEqualToString : @"blueColor" ]) {
color = [ UIColor blueColor ];
}
NSString *text = [dic objectForKey : @"text" ];
_text . text = text;
self . view . backgroundColor = color;
}
- ( void )didReceiveMemoryWarning
{
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
- ( void )dealloc
{
// 移除通知
[[ NSNotificationCenter defaultCenter ] removeObserver : self ];
[ _text release ];
[ super dealloc ];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end