通知传值(NSNotificationCenter)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenVveW91MTMxNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
通知传值
//流程:
1.注冊通知
2.通知中心,发送一条消息通知----------当中name名字千万不要写错了,会出如今3个地方
3.实现通知中心内部的方法,并实现传值
4.第四步,消息发送完,要移除掉
代码例如以下:
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "UIButton+Create.h"
@interface FirstViewController ()
{
UILabel * _label;
}
@end @implementation FirstViewController
- (void)dealloc
{
[_label release];
//第四步,消息发送完,要移除掉
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"labelTextNotification" object:nil];
[super dealloc];
}
- (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 redColor];
self.navigationItem.title = @"首页"; _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
_label.backgroundColor = [UIColor greenColor];
[self.view addSubview:_label]; UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Push" target:self action:@selector(didClickButtonAction)];
[self.view addSubview:button]; //第二步,通知中心,发送一条消息通知----------当中name名字千万不要写错了,会出如今3个地方
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showLabelText:) name:@"labelTextNotification" object:nil]; // Do any additional setup after loading the view.
} - (void)showLabelText:(NSNotification *)notification
{
//第三,实现通知中心内部的方法,并实现传值
id text = notification.object;
_label.text = text;
} - (void)didClickButtonAction
{ SecondViewController * secondVC = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
} - (void)didClick:(NSString *)text
{
_label.text = text;
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
#import "SecondViewController.h"
#import "FirstViewController.h"
#import "UIButton+Create.h"
@interface SecondViewController ()
{
UITextField * _textField;//创建一个输入框
}
@end
@implementation SecondViewController - (void)dealloc
{
[_textField release];
[super dealloc];
}
- (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 orangeColor];
self.navigationItem.title = @"第二页"; _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_textField]; UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Back" target:self action:@selector(didClickButtonAction)];
[self.view addSubview:button]; // Do any additional setup after loading the view.
} - (void)didClickButtonAction
{ //第一步注冊通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"labelTextNotification" object:_textField.text];
[self.navigationController popToRootViewControllerAnimated:YES];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenVveW91MTMxNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
通知传值(NSNotificationCenter)的更多相关文章
- iOS多视图传值方式之通知传值(NSNotification;NSNotificationCenter)
iOS传值方式之5:通知传值 第一需要发布的消息,再创建NSNotification通知对象,然后通过NSNotificationCenter通知中心发布消息(NSNotificationCenter ...
- iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值
有时候我们在页面跳转的时候回传递相应的参数,如,你想把在第一个页面的文本框里的内容显示在第二个文本框中,或者你又想把第二个文本框中的内容改变之后到第一个页面的文本框中,所有,这个时候我们就要用到页面跳 ...
- iOS pop使用通知传值
iOS pop回父级页面,使用通知传值 输入所要发送的信息 ,同时将label的值通过button方法调用传递, - (IBAction)buttonClick:(id)sender { //添加 字 ...
- iOS中通知传值
NSNotification 通知中心传值,可以跨越多个页面传值, 一般也是从后面的页面传给前面的页面. 思路: 第三个界面的值传给第一个界面. 1. 在第一个界面建立一个通知中心, 通过通知中心 ...
- IOS中通知中心(NSNotificationCenter)
摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广. IOS中通知中心NSNotificationCenter应用总结 一.了 ...
- 通知中心NSNotificationCenter的使用
通知中心NSNotificationCenter的使用 Cocoa框架中,通知中心以及KVO都属于设计模式中的观察者. Source 在使用通知中心之前,对通知中心类进行了简单的封装,以便可读性更强. ...
- iOS中通知中心NSNotificationCenter应用总结
通知中心(NSNotificationCenter)实际是在程序内部提供了一种广播机制.把接收到的消息,根据内部的消息转发表,将消息转发给需要的对象.这句话其实已经很明显的告诉我们要如何使用通知了.第 ...
- iOS 通知中心 NSNotificationCenter
iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...
- iOS开发之通知中心(NSNotificationCenter)
前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...
随机推荐
- wp检测是否是手机网络还是wifi网络
原文发布时间为:2013-06-22 -- 来源于本人的百度文章 [由搬家工具导入] ),newNameResolutionCallback(handle =>{NetworkInterface ...
- 取代VS, sourceISight的IDE神器CLION
https://www.jetbrains.com/clion/download/download-thanks.html 随时升级 http://idea.lanyus.com/ m_pRemoti ...
- (转)cygwin包管理器apt-cyg
通过终端安装apt-cyg之前选要安装以下软件包 wget tar gawk bzip2 Cygwin终端安装 wget http://apt-cyg.googlecode.com/svn/trunk ...
- linux进程地址空间--vma的基本操作【转】
转自:http://blog.csdn.net/vanbreaker/article/details/7855007 版权声明:本文为博主原创文章,未经博主允许不得转载. 在32位的系统上,线性地址空 ...
- django中的类视图
# 原创,转载请留言联系 当我们在开发一个注册模块时.浏览器会通过get请求让注册表单弹出来,然后用户输完注册信息后,通过post请求向服务端提交信息.这时候我们后端有两个视图函数,一个处理get请求 ...
- 使用EventHandler传递参数
1.MouseEventHandler和EventHandler传递参数的局限性分析 开发过程中,特别是使用自定义控件时,常常需要对一个控件的click,mouseDown,mouseUp等事件的处理 ...
- AC日记——Little Elephant and Shifts codeforces 221e
E - Little Elephant and Shifts 思路: 一次函数线段树(疯狂debug): b不断循环左移,判断每次最小的|i-j|,a[i]=b[j]: 仔细观察发现,每个bi移动时, ...
- HDU 2063.过山车-Hungary(匈牙利算法)
过山车 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- (1)php开篇常识
一.php PHP由zend公司开发维护,目前最高版本PHP7.1,官网地址http://php.net/ PHP从5.5开始不支持XP,没有PHP6这个版本 PHP是嵌入到HTML的脚本代码,格式: ...
- luogu 2509. 森林大礼包
2509. 森林大礼包 ★ 输入文件:three_squirrels.in 输出文件:three_squirrels.out 简单对比时间限制:1 s 内存限制:256 MB [题目描 ...