通知传值(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)
前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...
随机推荐
- codechef AUG17 T1 Chef and Rainbow Array
Chef and Rainbow Array Problem Code: RAINBOWA Chef likes all arrays equally. But he likes some array ...
- Handler 源码分析
Handler用法: 无参 Handler 构造函数实例化一个 Handler 类型的全局变量,并重写其 handleMessage 方法,在某一方法内调用 Handler 的 sendEmptyMe ...
- struts2 json 输出日期格式不正确
struts2 输出json中 日期出现:2013-12-17T15:57:47 错误格式的数据 原因:struts2 json插件对日期的格式化有问题 解决方法:在实体类的日期的get方法上加注解: ...
- Android控件介绍
1. 介绍 Android控件大多位于android.widget, android.view.View为他们的父类对于Dialog系列, android.app.Dialog为父类 Android的 ...
- android hook 框架 libinject2 简介、编译、运行
Android so注入-libinject2 简介.编译.运行 Android so注入-libinject2 如何实现so注入 Android so注入-Libinject 如何实现so注入 A ...
- Appium+python自动化13-native和webview切换【转载】
前言 现在大部分app都是混合式的native+webview,对应native上的元素通过uiautomatorviewer很容易定位到,webview上的元素就无法识别了. 一.识别webview ...
- (1)php开篇常识
一.php PHP由zend公司开发维护,目前最高版本PHP7.1,官网地址http://php.net/ PHP从5.5开始不支持XP,没有PHP6这个版本 PHP是嵌入到HTML的脚本代码,格式: ...
- Codeforces Round #270 A. Design Tutorial: Learn from Math【数论/埃氏筛法】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Cosmos DB
类似的数据库还有Google的Spanner. 参考:官网说明 另一个介绍 Key capabilities As a globally distributed database service, A ...
- java collection get 方法
Collections unmodifiableList