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这 ...
随机推荐
- [转]MVC之 自定义过滤器(Filter)
本文转自:http://www.cnblogs.com/kissdodog/archive/2013/01/21/2869298.html 一.自定义Filter 自定义Filter需要继承Actio ...
- Spark性能优化指南-高级篇(spark shuffle)
Spark性能优化指南-高级篇(spark shuffle) 非常好的讲解
- taskctl命令行类(sh、exe、python新增scp)插件升级扩展
转载自: http://www.taskctl.com/forum/detail_129.html 上次写了一个帖子 TASKCTL中不使用代理,通过ssh免密连接执行远程脚本配置(SSH插件扩展)h ...
- mongodb数据库命令
常用数据库命令汇总 Database Commands Api 下面简单列一下Shell常用的基本命令 启动连接Mongodb #带配置信息启动 mongod -f xxx.conf #连接 mong ...
- px-em-rem单位转换
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- DB2 系统命令与配置参数大全
主要包括4个部分,分别为: DB2 系统命令 DB2 数据库管理器配置参数 DB2 数据库系统配置参数 DB2 管理服务器(DAS)配置参数DB2 系统命令 dasauto - 自动启动 DB2 管理 ...
- 字符流-缓冲区-自定义myBufferedReader
public class myBufferedReaderDemo { public static void main(String[] arg) throws IOException{ FileRe ...
- 前端请求操作类型(get post put delete)
get:获取数据 post:增加 put:修改 delete:删除
- 当点阵字库遇到3D
早在遥远的DOS时代,点阵汉字库为计算机处理汉字起到了关键作用.当时的显示器在图形模式下的分辨率只有640x480甚至320x200,显示汉字直接使用点阵字库在屏幕上打点就可以了.如今的电脑屏幕甚至手 ...
- 什么是 C 和 C ++ 标准库?
简要介绍编写C/C ++应用程序的领域,标准库的作用以及它是如何在各种操作系统中实现的. 我已经接触C++一段时间了,一开始就让我感到疑惑的是其内部结构:我所使用的内核函数和类从何而来? 谁发明了它们 ...