4、iOS传值的方法-Notifaction反向传值
2015-07-17 16:43
417 查看
iOS传值,用Notifaction反向传值,将B界面的值传到A界面。在A界面注册一个通知,在B界面发送一个通知。
1、A类中的代码如下:
2、B类中的代码如下:
3、效果截图如下:
4、Demo下载地址 : http://download.csdn.net/detail/u010545480/8909569
1、A类中的代码如下:
#import "FirstViewController.h" #import "SecondViewController.h" @interface FirstViewController () @property (retain, nonatomic) UILabel *label; @end @implementation FirstViewController @synthesize label; - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.title = @"Notifaction反向传值"; label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 30)]; label.text = @"点击按钮"; [self.view addSubview:label]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, self.view.frame.size.width-20, 35)]; btn.backgroundColor = [UIColor grayColor]; [btn setTitle:@"确定" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; //注册一个通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(flashString:) name:@"flash" object:nil]; } - (void)flashString:(NSNotification *)notification { NSDictionary *dict = [notification userInfo]; label.text = [dict objectForKey:@"name"]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)btnClick:(id)sender { SecondViewController *second = [[SecondViewController alloc] init]; [self.navigationController pushViewController:second animated:YES]; } @end
2、B类中的代码如下:
#import "SecondViewController.h" @interface SecondViewController () @property (retain,nonatomic) UITextField *textField; @end @implementation SecondViewController @synthesize textField; - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 30)]; textField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:textField]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 150, self.view.frame.size.width-20, 35)]; btn.backgroundColor = [UIColor grayColor]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnClick:(id)sender { //发送一个通知 [[NSNotificationCenter defaultCenter] postNotificationName:@"flash" object:self userInfo:@{@"name":textField.text}]; [self.navigationController popViewControllerAnimated:YES]; } @end
3、效果截图如下:
4、Demo下载地址 : http://download.csdn.net/detail/u010545480/8909569
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- c#窗体传值用法实例详解
- ASP.NET页面间的传值的几种方法
- 关于C语言中参数的传值问题
- C# Winform窗口之间传值的多种方法浅析
- php下通过POST还是GET来传值
- JS中Iframe之间传值的方法
- asp.net 页面间传值方法小结
- ASP.NET页面传递值的方式介绍
- Servlet与JSP间的两种传值情况
- javascript中的变量是传值还是传址的?
- Javascript showModalDialog两个窗体之间传值
- Android学习笔记--使用剪切板在Activity中传值示例代码
- 如何传值在2个页面之间 要求不刷新父页面,并且不能用Querystring传值