NSNotificationCenter 通知中心传值

1NSNotification

这个类可以理解为一个消息对象,其中有三个成员变量。

这个成员变量是这个消息对象的唯一标识,用于辨别消息对象。

@property (readonly, copy) NSString *name;

 

这个成员变量定义一个对象,可以理解为针对某一个对象的消息。

@property (readonly, retain) id object;

 

这个成员变量是一个字典,可以用其来进行传值。

@property (readonly, copy) NSDictionary *userInfo;

 几点注意:

1、如果发送的通知指定了object对象,那么观察者接收的通知设置的object对象与其一样,才会接收到通知,但是接收通知如果将这个参数设置为了nil,则会接收一切通知。

2、观察者的SEL函数指针可以有一个参数,参数就是发送的死奥西对象本身,可以通过这个参数取到消息对象的userInfo,实现传值。

 

首先我们要确认那边要传值那边要接受传过来的值,

在传值的一方我们要写一个创建一个消息 NSNotification ,并且用通知中心NSNotificationCenter 发送这个消息

接收传过来的值这里我们要创建一个通知中心NSNotificationCenter 并且添加观察者,在添加观察者这个方法里面有一个响应事件的方法  我们可以在响应事件的方法里面接收传过来的值

 

好啦我们开始写代码吧

传值的一方在这个控制器里面我们创建一个UITextFiled UITextfiledtext传到上一个控制器的UIlabel上显示出来

#import "PYJViewController.h"

@interface PYJViewController ()
@property (nonatomic,strong) UITextField *textField;
@end

@implementation PYJViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton * button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame =CGRectMake(100, 100, 100, 30);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    button.backgroundColor=[UIColor yellowColor];
    [button addTarget:self action:@selector(backLastPage:) forControlEvents:UIControlEventTouchUpInside];
    self.textField=[[UITextField alloc]init];
    self.textField.frame=CGRectMake(100, 150, 200, 30);
    self.textField.borderStyle=UITextBorderStyleRoundedRect;

    [self.view addSubview:self.textField];

    [self.view addSubview:button];
    self.view.backgroundColor=[UIColor whiteColor];

}

- (void)backLastPage:(UIButton *)bt{

    //创建一个消息对象
    NSNotification * notice =[NSNotification notificationWithName:@"notice" object:nil userInfo:@{@"text":self.textField.text}];
    //发送消息
    [[NSNotificationCenter defaultCenter]postNotification:notice];

    [self.navigationController popViewControllerAnimated:YES];

}

接收传值的控制器:

#import "ViewController.h"
#import "PYJViewController.h"
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)buttonAction:(UIButton *)sender;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.label.text=@"测试一";

    //获取通知中心
    NSNotificationCenter * center =[NSNotificationCenter defaultCenter];

    //添加观察者 Observer表示观察者  reciveNotice:表示接收到的消息  name表示再通知中心注册的通知名  object表示可以相应的对象 为nil的话表示所有对象都可以相应
    [center addObserver:self selector:@selector(reciveNotice:) name:@"notice" object:nil];

}

- (void)reciveNotice:(NSNotification *)notification{

    NSLog(@"收到消息啦!!!");

    self.label.text=[notification.userInfo objectForKey:@"text"];

}

- (IBAction)buttonAction:(UIButton *)sender {

    PYJViewController * vc=[[WBBViewController alloc]init];

    [self.navigationController pushViewController:vc animated:YES];

}
时间: 2024-08-02 10:59:21

NSNotificationCenter 通知中心传值的相关文章

学习IOS开发UI篇--NSNotificationCenter通知中心

NSNotificationCenter 较之于 Delegate 可以实现更大的跨度的通信机制,可以为两个无引用关系的两个对象进行通信.NSNotificationCenter 的通信原理使用了观察者模式; 1. NSNotificationCenter 注册观察者对某个事件(以字符串命名)感兴趣,及该事件触发时该执行的 Selector 或 Block 2. NSNotificationCenter 在某个时机激发事件(以字符串命名) 3. 观察者在收到感兴趣的事件时,执行相应的 Selec

iOS之NSNotificationCenter通知中心使用事项

其实这里的通知和之前说到的KVO功能很想,也是用于监听操作的,但是和KVO不同的是,KVO只用来监听属性值的变化,这个发送监听的操作是系统控制的,我们控制不了,我们只能控制监听操作,类似于Android中系统发送的广播,我们只能接受.但是通知就不一样了,他的监听发送也是又我们自己控制,我们可以在任何地方任何时机发送一个通知,类似于Android中开发者自己发送的广播.从这一点看来,通知的使用场景更为广泛了. 下面就来看一下例子: 还是护士和小孩的那个例子 Children.h [objc] vi

用通知中心传值时候的注意事项

当用通知中心 去传值的时候  通常 传的 都是字典 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSDictionary *dic=[defaults objectForKey:@"forcedUpdateResponseObject2"]; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc  postNotif

iOS 设计模式-NSNotificationCenter 通知中心

通知介绍 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发布通知(NSNotification),描述自己在做什么. 其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知 初始化通知中心 // 初始化通知中心 NSNotificationCenter *center =[NSNotificationCenter defaultCenter];

UI_通知中心_传值的一种方法

NSNotification 通知中心传值,可以跨越多个页面传值, 一般也是从后面的页面传给前面的页面. 思路: 第三个界面的值传给第一个界面. 1. 在第一个界面建立一个通知中心, 通过通知中心,注册一个监听事件 2. 在第一个界面中,设置接收到通知的事件. 3. 在第一个界面中的dealloc中, 将通知中心remove掉 4. 在第三个界面中, 建立一个通知中心, 通过通知中心, 发送通知(发送通知的过程就是传值的过程,将要传输的值作为object的值传给第一个界面 代码片段: 第一界面:

通知中心 NSNotificationCenter 的简单使用方法

NSNotificationCenter(通知中心)   [注意]需再dealloc中移除观察者   获取通知中心单例对象 NSNotificationCenter *center=[NSNotificationCenter defaultCenter]; 常用方法: 1.注册观察者 - (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject; 参数: ob

iOS 通知中心 NSNotificationCenter

iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotification 可以理解为消息对象,包含三个成员变量,如下: @property (readonly, copy) NSString *name; @property (nullable, readonly, retain) id object; @property (nullable, readonl

Notification Centers 通知中心

Notification Centers 通知中心 A notification center manages the sending and receiving of notifications. It notifies all observers of notifications meeting specific criteria. The notification information is encapsulated in NSNotification objects. Client o

通知中心NSNotification与委托的异同,需要注意的要点

调度表 通知中心保存了一个调度表,表的内容包括:通知观察者(必须存在).通知名称和通知发送者. 通知中心的调度表给观察者指定了对应的通知集,一个通知集是通知中心发出的通知的子集. 调度表入口有4种类型,如下表所示: (英文版说明) Notification name Notification sender Notification set specified Specified Specified Notifications with a particular name from a speci