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)的更多相关文章

  1. iOS多视图传值方式之通知传值(NSNotification;NSNotificationCenter)

    iOS传值方式之5:通知传值 第一需要发布的消息,再创建NSNotification通知对象,然后通过NSNotificationCenter通知中心发布消息(NSNotificationCenter ...

  2. iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值

    有时候我们在页面跳转的时候回传递相应的参数,如,你想把在第一个页面的文本框里的内容显示在第二个文本框中,或者你又想把第二个文本框中的内容改变之后到第一个页面的文本框中,所有,这个时候我们就要用到页面跳 ...

  3. iOS pop使用通知传值

    iOS pop回父级页面,使用通知传值 输入所要发送的信息 ,同时将label的值通过button方法调用传递, - (IBAction)buttonClick:(id)sender { //添加 字 ...

  4. iOS中通知传值

    NSNotification 通知中心传值,可以跨越多个页面传值, 一般也是从后面的页面传给前面的页面.   思路: 第三个界面的值传给第一个界面. 1. 在第一个界面建立一个通知中心, 通过通知中心 ...

  5. IOS中通知中心(NSNotificationCenter)

    摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广.   IOS中通知中心NSNotificationCenter应用总结 一.了 ...

  6. 通知中心NSNotificationCenter的使用

    通知中心NSNotificationCenter的使用 Cocoa框架中,通知中心以及KVO都属于设计模式中的观察者. Source 在使用通知中心之前,对通知中心类进行了简单的封装,以便可读性更强. ...

  7. iOS中通知中心NSNotificationCenter应用总结

    通知中心(NSNotificationCenter)实际是在程序内部提供了一种广播机制.把接收到的消息,根据内部的消息转发表,将消息转发给需要的对象.这句话其实已经很明显的告诉我们要如何使用通知了.第 ...

  8. iOS 通知中心 NSNotificationCenter

    iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...

  9. iOS开发之通知中心(NSNotificationCenter)

    前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...

随机推荐

  1. Java笔记(一)

    1. ConcurrentModificationException 在遍历容器的同时修改容器里的成员对象可能会抛出该异常 http://www.blogjava.net/EvanLiu/archiv ...

  2. keystone总结

    1. Keystone(OpenStack Identity Service)是OpenStack框架中,负责身份验证.服务规则和服务令牌的功能, 它实现了OpenStack的Identity API ...

  3. 结构型设计模式之装饰模式(Decorator)

    结构 意图 动态地给一个对象添加一些额外的职责.就增加功能来说,D e c o r a t o r 模式相比生成子类更为灵活. 适用性 在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职责. ...

  4. loj 数列分块入门 5 7 8

    5 题意 给出一个长为\(n\)的数列,以及\(n\)个操作,操作涉及区间开方,区间求和. 思路 用\(tag\)记录这一块是否已全为\(1\). 除分块外,还可用 树状数组+并查集(链表) 或者 线 ...

  5. 积木大赛(NOIP2013)(纯贪心+模拟)

    好吧,这道题也是..醉了. 其实题目编程挺水的,但是贪心过程不好想. 原题传送门 这道题对于任何一个点a[i]如果a[i]<a[i-1]的话,那么假设a[i-1]的高度为X,a[i]的高度为y, ...

  6. 【程序打包工具 Inno Setup】CreateProcess 失败:代码 740(Inno Setup打包的程序提升为管理员权限)

    原文参考 https://www.cnblogs.com/SnailProgramer/p/4243666.html http://blog.csdn.net/x356982611/article/d ...

  7. Python的功能模块[4] -> pdb/ipdb -> 实现 Python 的单步调试

    pdb / ipdb 模块 / pdb / ipdb Module pdb 和 ipdb 的主要作用是用于 Python 程序的单步调试,Python 的调试可参考链接. 下面是一个简单的使用示例 i ...

  8. 模板—字符串—AC自动机(多模式串,单文本串)

    模板—字符串—AC自动机(多模式串,单文本串) Code: #include <queue> #include <cstdio> #include <cstring> ...

  9. Frequency

    题目描述 Snuke loves constructing integer sequences. There are N piles of stones, numbered 1 through N. ...

  10. Xamarin XAML语言教程构建进度条ProgressBar

    Xamarin XAML语言教程构建进度条ProgressBar Xamarin XAML语言教程构建进度条ProgressBar,ProgressBar被称为进度条,它类似于没有滑块的滑块控件.进度 ...