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

在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. 279 Perfect Squares 完美平方数

    给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n.你需要让平方数的个数最少.比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 : ...

  2. 关于.Net中Process的使用方法和各种用途汇总(一):Process用法简介

    简介: .Net中Process类功能十分强大.它可以接受程序路径启动程序,接受文件路径使用默认程序打开文件,接受超链接自动使用默认浏览器打开链接,或者打开指定文件夹等等功能. 想要使用Process ...

  3. SQL 几个查看性能的语句

    1.查找目前SQL Server所执行的SQL语法,并展示资源情况: SELECT s2.dbid , DB_NAME(s2.dbid) AS [数据库名] , --s1.sql_handle , ( ...

  4. mysql timeout expired处理

    一.发现问题 二.分析问题 .net长时间连接mysql导致超时: 方式一:连接用完后,就关闭连接 方式二:增加C#的执行sqlcommand时间 三.解决问题 增加了这一句,问题解决了 using ...

  5. 329.-io流(字符-练习-复制文本文件二)

    //每次读取的字节长度,一般都是1024的倍数 private static final int BUF_SIZE = 1024; public static void main(String[] a ...

  6. 如何让一个div里面的div垂直居中?

    如何让一个div里面的div垂直居中? 如何让上面灰色有文字那个div和背景图标垂直居中,不管屏幕大小有好大,始终在垂直方向上的中间.上面有整个布局和样式表,谢谢高手指点 CSS3时代当然要用CSS3 ...

  7. BZOJ 3884: 上帝与集合的正确用法 扩展欧拉定理 + 快速幂

    Code: #include<bits/stdc++.h> #define maxn 10000004 #define ll long long using namespace std; ...

  8. radiobutton group

    1. 环境:VS2010 2. 分组 将radio1.radio2.radio3分为1组,radio4.radio5分为另一组: 方法:设置  radio1  的 属性:  group.tabstop ...

  9. CAD使用SetxDataString写数据(网页版)

    主要用到函数说明: MxDrawEntity::SetxDataString 写一个字符串扩展数据,详细说明如下: 参数 说明 [in] BSTR val 字符串值 szAppName 扩展数据名称 ...

  10. CAD插入图片

    在CAD设计绘图时,需要插入外部图片,可以设置图片的缩放比例.旋转角度.图片显示文件名等属性. 主要用到函数说明: _DMxDrawX::DrawImageMark 绘图制一个图象标记对象.详细说明如 ...