Snail—UI学习之自己定义通知NSNotification
背景是:一个界面跳转到第二个界面 然后 第一个界面发了一个通知 然后第二个界面收到这个通知后 把里面的数据取出来
在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的更多相关文章
- Snail—UI学习之自己定义标签栏UITabBarController
这里的背景跟上面的差点儿相同 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkF ...
- (七十二)自己定义通知NSNotification实现消息传递
众所周知,iOS中一般在类之间传递消息使用较多的是delegate和block,另一种是基于通知进行的消息传递,我们经常是使用系统的通知.来实现一些功能.比如利用键盘尺寸改变的通知,我们能够依据键盘的 ...
- Snail—UI学习之得到某组件的方法
第一种方法:依据传入函数的參数对象的tag属性区分 比方 多个button运行同一个方法,可是不同的方法运行时.里面的逻辑又不一样 那就得加以区分 这时能够用tag来差别 //再新建一个Button ...
- Snail—UI学习之UITextField
简单看一下UITextField的属性 - (void)createTextField{ UITextField * textField = [[UITextField alloc] initWith ...
- Snail—UI学习之导航视图控制器UINavigationController(系统)
背景 有一个根视图控制器 然后跳转到第一个界面 第一个界面能够返回到根视图 也能够跳转到第二个视图 第二个视图能够直接返回到根视图 新建三个ViewController RootViewCon ...
- Android UI学习 - ListView (android.R.layout.simple_list_item_1是个什么东西)
Android UI学习 - ListView -- :: 标签:Android UI 移动开发 ListView ListActivity 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始 ...
- iOS利用通知(NSNotification)进行传值
通知 是在跳转控制器之间常用的传值代理方式,除了代理模式,通知更方便.便捷,一个简单的Demo实现通知的跳转传值. iOS通知传值的使用 输入所要发送的信息 ,同时将label的值通过button方法 ...
- IOS开发中如何使用通知NSNotification传值
通知 是在跳转控制器之间常用的传值代理方式,除了代理模式,通知更方便.便捷,一个简单的Demo实现通知的跳转传值. 输入所要发送的信息 ,同时将label的值通过button方法调用传递, - (IB ...
- IOS开发-UI学习-sqlite数据库的操作
IOS开发-UI学习-sqlite数据库的操作 sqlite是一个轻量级的数据库,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了,而且它的处理速度比Mysql.PostgreSQL这 ...
随机推荐
- select多选
1.css <style> .divBox{ width:400px; margin:100px auto; } .divBox span{ vertical-align:top; dis ...
- javascript异步下载 Promise实现
一般下载都是直接打开一个链接就行.var URL = 'XXXX';window.open(URL)其实这样会有些问题:1. 浏览器禁止打开新窗口,导致无法下载 那么怎么解决呢?这样: <a h ...
- struts2之actionSupport学习
actionSupport在手工完成字段验证,显示错误消息,国际化等情况下推荐使用.
- JDBC使用游标实现分页查询的方法
本文实例讲述了JDBC使用游标实现分页查询的方法.分享给大家供大家参考,具体如下: /** * 一次只从数据库中查询最大maxCount条记录 * @param sql 传入的sql语句 * @par ...
- Spring+Spring MVC+Hibernate增查(使用注解)
使用Spring+Spring MVC+Hibernate做增删改查开发效率真的很高.使用Hibernate简化了JDBC连接数据库的的重复性代码.下面根据自己做的一个简单的增加和查询,把一些难点分析 ...
- (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
http://blog.csdn.net/yerenyuan_pku/article/details/72863323 我们知道Jedis在处理Redis的单机版和集群版时是完全不同的,有可能在开发的 ...
- vue组件---插槽
(1)插槽内容 Vue 实现了一套内容分发的 API,这套 API 的设计灵感源自 Web Components 规范草案,将 <slot> 元素作为承载分发内容的出口. 在父级组件里可以 ...
- returnValue of Chrome
说实话,我一看到这个returnValue就有点反感,感觉这个就是IE式的老套的用法,因为项目中有用到就了解了下,以下主要是一些我的理解和发现吧. PS:returnValue是window的属性,s ...
- 洛谷——P1073 最优贸易
P1073 最优贸易 n 个城市间以 m 条有向道路连接, 小 T 从 1 号城市出发, 将要去往 n 号城市.小 T 观察到一款商品 Z 在不同的城市的价格可能不尽相同,小 T 想要在旅行中的某一个 ...
- 洛谷——P2090 数字对
P2090 数字对 题目描述 对于一个数字对(a, b),我们可以通过一次操作将其变为新数字对(a+b, b)或(a, a+b). 给定一正整数n,问最少需要多少次操作可将数字对(1, 1)变为一个数 ...