IOS:OC--通知传值

作者: 任任任任师艳 | 来源:发表于2017-06-03 07:50 被阅读0次

1:
新建两个类都是继承自 UIViewController,取名为“FirstViewController"和
"SecondViewController"

AppDelegate.m
将 FirstViewController作为根视图控制器
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[FirstViewController new]];
self.window.backgroundColor = [UIColor yellowColor];
[self.window makeKeyAndVisible];

FirstViewController.m 导入头文件#import "SecondViewController.h"

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor yellowColor];
    [self creatUI];

    //第二步:谁接受消息谁注册通知(注册监听者)
    //注册监听者
    //addObserver:监听者 ,一般都是控制器本身去监听通知
    //selector:当监听到通知中心发送的通知时,执行的方法
    //name:当通知中心的名字,要和发送通知对象的名字一致
    //object
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeTextFtext:) name:@"ChangeValue" object:nil];

}
//实现通知方法
-(void)ChangeTextFtext:(NSNotification *)notifition{

//获取通知的内容
NSDictionary * dic = notifition.userInfo;
NSLog(@"%@",dic);
self.field.text = dic[@"Data"];

}
-(void)creatUI{

self.field = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30) ];
self.field.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.field];

UIButton * pushbutton =[UIButton buttonWithType:UIButtonTypeSystem];
pushbutton.frame = CGRectMake(0, 140, self.view.frame.size.width, 30);
[pushbutton setTitle:@"Push" forState:(UIControlStateNormal)];
[pushbutton addTarget:self action:@selector(PushClick:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:pushbutton];

}
-(void)PushClick:(UIButton *)sender{

SecondViewController * secondVC = [SecondViewController new];
//跳转:
[self.navigationController pushViewController:secondVC animated:YES];

}
/*
通知传值:
1.需要发送通知的物体
2.接收者:谁监听谁接收(注册通知--》name前后一定要一样)
3.实现通知内部方法,并实现传值
4.消息发送完毕,移除监听者
*/

SecondViewController.m:
导入文件#import "FirstViewController.h"

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor cyanColor];

    self.field = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30) ];
    self.field.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.field];

    UIButton * popbutton =[UIButton buttonWithType:UIButtonTypeSystem];
    popbutton.frame = CGRectMake(0, 140, self.view.frame.size.width, 30);
    [popbutton setTitle:@"Pop" forState:(UIControlStateNormal)];
    [popbutton addTarget:self action:@selector(PopClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:popbutton];

}
-(void)PopClick:(UIButton *)sender{

//发送通知:    单例default\share
//postNotificationName通知中心名字   object接收对象  userInfo携带的参数


[[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeValue" object:nil userInfo:@{@"Data":self.field.text}];

// FirstViewController * firstVC = [FirstViewController new];
//跳转:
[self.navigationController popViewControllerAnimated:YES];

}

相关文章

  • IOS:OC--通知传值

    1:新建两个类都是继承自 UIViewController,取名为“FirstViewController"和"S...

  • iOS 通知

    iOS 通知传参使用方法 尽量不要在viewWillDisappear:方法中移除通知 iOS通知传值的使用 1、...

  • iOS的五种传值

    前言 iOS常见的五种传值分别为属性传值,通知传值,代理传值,block传值,单例传值 属性传值 用于正向传值,简...

  • iOS 常用传值方式

    总结 iOS 日常开发中的几种常用传值方式:正向传值代理传值block传值通知传值单例 文章代码:https://...

  • ioS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • iOS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • Block传值

    iOS传值一共有四种:属性传值,代理传值,通知传值以及Block传值; 今天我们来说一下Block传值: 概念:带...

  • iOS 传值方法(属性传值、代理传值、Block、通知、单例)

    iOS 传值方法(属性传值、代理传值、Block、通知、单例)简单的介绍一下几个传值方式 1、属性传值 在传值的时...

  • iOS传值的几种常用方式

    iOS常用的传值方式有以下几种: 属性传值、单例传值、代理传值、block传值、通知传值 接下来我就分别讲述一下这...

  • 【iOS】通知传值

    一、应用场景: 通知中心传值,可以跨越多个页面传值, 一般也是从后面的页面传给前面的页面。 二、实现思路: 第三个...

网友评论

    本文标题:IOS:OC--通知传值

    本文链接:https://www.haomeiwen.com/subject/rjypfxtx.html