背景是:一个界面跳转到第二个界面 然后 第一个界面发了一个通知  然后第二个界面收到这个通知后 把里面的数据取出来

在RootViewController.m中写入以下代码

#import "WJJRootViewController.h"
#import "WJJFirstViewController.h" @interface WJJRootViewController (){
UITextField * _textField;
} @end @implementation WJJRootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
// Do any additional setup after loading the view.
_textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 40, 240, 30)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.placeholder = @"请输入:";
_textField.clearButtonMode = UITextFieldViewModeAlways;
_textField.keyboardType = UIKeyboardTypeDefault;
_textField.returnKeyType = UIReturnKeyGo; //设置textField的代理 要让viewController为他收起键盘
_textField.delegate = self; [self.view addSubview:_textField];
[self createButton];
} - (void)createButton{ UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 80 , 50, 50);
button.backgroundColor = [UIColor blackColor];
[button setTitle:@"点我" forState:UIControlStateNormal];
[button addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button]; } - (void)nextPage{
//新建一个界面
WJJFirstViewController * firstViewController = [[WJJFirstViewController alloc] init];
//设置反转风格
firstViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//从第一个界面转到第二个界面 有动画
[self presentViewController:firstViewController animated:YES completion:nil]; //发一个通知 假设第二个界面接收这个通知的话,就会得到通知里面的数据
//写在跳转界面之后 这样跳转后第二个界面才干够接收
[[NSNotificationCenter defaultCenter] postNotificationName:@"1523" object:_textField.text];
}

//那么在第二个界面中就要接收这个通知的话 代码例如以下

#import "WJJFirstViewController.h"

@interface WJJFirstViewController (){
UILabel * _label;
} @end @implementation WJJFirstViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_label = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 200, 50)];
_label.backgroundColor = [UIColor grayColor];
[self.view addSubview:_label];
//get:方法是接收到通知后 做得操作 name:相似频道 在这个频道上就能接收到这个通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(get:) name:@"1523" object:nil];
} - (void)get:(NSNotification *)noti{ //得到通知里面的对象
id obj = noti.object;
NSString * str = (NSString *)obj;
_label.text = str; } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

效果例如以下

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

Snail—UI学习之自己定义通知NSNotification的更多相关文章

  1. Snail—UI学习之自己定义标签栏UITabBarController

    这里的背景跟上面的差点儿相同 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkF ...

  2. (七十二)自己定义通知NSNotification实现消息传递

    众所周知,iOS中一般在类之间传递消息使用较多的是delegate和block,另一种是基于通知进行的消息传递,我们经常是使用系统的通知.来实现一些功能.比如利用键盘尺寸改变的通知,我们能够依据键盘的 ...

  3. Snail—UI学习之得到某组件的方法

    第一种方法:依据传入函数的參数对象的tag属性区分 比方 多个button运行同一个方法,可是不同的方法运行时.里面的逻辑又不一样 那就得加以区分 这时能够用tag来差别 //再新建一个Button ...

  4. Snail—UI学习之UITextField

    简单看一下UITextField的属性 - (void)createTextField{ UITextField * textField = [[UITextField alloc] initWith ...

  5. Snail—UI学习之导航视图控制器UINavigationController(系统)

    背景 有一个根视图控制器 然后跳转到第一个界面  第一个界面能够返回到根视图 也能够跳转到第二个视图 第二个视图能够直接返回到根视图 新建三个ViewController    RootViewCon ...

  6. Android UI学习 - ListView (android.R.layout.simple_list_item_1是个什么东西)

    Android UI学习 - ListView -- :: 标签:Android UI 移动开发 ListView ListActivity 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始 ...

  7. iOS利用通知(NSNotification)进行传值

    通知 是在跳转控制器之间常用的传值代理方式,除了代理模式,通知更方便.便捷,一个简单的Demo实现通知的跳转传值. iOS通知传值的使用 输入所要发送的信息 ,同时将label的值通过button方法 ...

  8. IOS开发中如何使用通知NSNotification传值

    通知 是在跳转控制器之间常用的传值代理方式,除了代理模式,通知更方便.便捷,一个简单的Demo实现通知的跳转传值. 输入所要发送的信息 ,同时将label的值通过button方法调用传递, - (IB ...

  9. IOS开发-UI学习-sqlite数据库的操作

    IOS开发-UI学习-sqlite数据库的操作 sqlite是一个轻量级的数据库,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了,而且它的处理速度比Mysql.PostgreSQL这 ...

随机推荐

  1. fiddler不同代理模式的区别

    Fiddler有不同的代理模式,分为以下两种: 流模式(Streaming)和缓冲模式(Buffering). 流模式可以理解为一种实时通信的模式,有请求就有返回,也就是实时返回. 缓冲模式是等所有请 ...

  2. c++类的内存布局

    问题: 考察了reinterpret_cast和static_cast的区别.顺道发现了一个可以查看c++内存布局的工具(在VS中). 结果: 前两个输出的地址形同,后一个不同. class A{in ...

  3. Windows Install Twisted 安装Twisted

    1.下载twisted exe https://twistedmatrix.com/Releases/Twisted/15.4/ (注意最新版16.x没有适用于windows的exe,只能用旧版) 2 ...

  4. js类型识别

    typeof总结: 可以识别标准类型(Null除外) 不能识别具体的对象类型(Function除外) Object.prototype.toString总结: 可以识别标准类型和内置对象类型 不能识别 ...

  5. Win7 下 PB (PowerBuilder) Insert Control 崩溃的解决办法

    环境: WIN7 x86  PB8.0, x64系统目录不同,不过也可以试试 Insert -> OLE... -> Insert Control  - 崩溃 如果网上提供的办法解决不了你 ...

  6. HDU_1180_诡异的楼梯_BFS

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1180 诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Me ...

  7. ThinkPHP---TP功能类之公文管理功能

    [一]准备工作 (1)创建数据表 表名:sp_doc create table sp_doc( id int(11) not null auto_increment, title varchar(50 ...

  8. 关于DOS-BOX的使用方法

    将MASM文件夹里的全部文件拷贝到一个目录下,比如E:\masm下,然后将这个目录挂着为DOSBox的一个盘符下,挂载命令为 Mount c e:\masm 切换到E盘 然后编译,运行

  9. spring cloud (一):大话 Spring Cloud

    转自:http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html 研究了一段时间Spring Boot了准备向Spr ...

  10. Gym - 101670C Chessboard Dancing(CTU Open Contest 2017 找规律)

    题目:链接 思路: 多画出几个情况就可以找出规律来了 Knight (当大于2的时候只要两种颜色相间出现就可以了) King(当大于等于3的时候,总可以用四种形式来补色,具体如下)  Bishop(斜 ...