iOS页面传值-wang
iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
实现了以下iOS页面间传值:1.委托delegate方式;2.通知notification方式;3.block方式;4.UserDefault或者文件方式;5.单例模式方式;6.通过设置属性,实现页面间传值
在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一下:
情况1:A页面跳转到B页面
方法:
在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给B的属性赋值即可
1.//SecondViewController.h1.@property(nonatomic) NSInteger flag;//当前系统标示(0:其他传值方式;1:block传值方式)在A页面的试图控制器中
1.//RootViewController.m1.- (IBAction)showSecondView:(id)sender {2.SecondViewController *second = [[SecondViewController alloc] initWithNibName:@'SecondViewController' bundle:nil];3.second.delegate = self;4.second.flag = 0;5.[self presentViewController:second animated:YES completion:nil];6.}情况2:A页面跳转到B页面,B页面再跳转回A页面
主流方案:
(1)通过委托delegate的方式实现
设置协议及方法
1. 1.//SecondViewController.h1.@protocol secondViewDelegate2.-(void)showName:(NSString *)nameString;3.@end设置代理(为防止循环引用,此处采用了weak)
1. 1.//SecondViewController.h1.@interface SecondViewController : UIViewController2.@property (nonatomic, weak)id<secondViewDelegate> delegate;3.@property (nonatomic, copy) ablock block;4.@end调用
01.//SecondViewController.m02.- (IBAction)delegateMethod:(id)sender {03.if ([self notEmpty]) {04.[self.delegate showName:self.nameTextField.text];05.[self dismissViewControllerAnimated:YES completion:nil];06.}else{07.[self showAlert];08.}09.}显示
1.//RootViewController.m2.-(void)showName:(NSString *)nameString{3.self.nameLabel.text = nameString;4.}最重要也是最容易忽略的,就是一定要设置delegate的指向。
(2)通过通知notification的方式实现
在B页面的控制器中,发送通知:
01.//SecondViewController.m02.- (IBAction)notificationMethod:(id)sender {03.if ([self notEmpty]) {04.[[NSNotificationCenter defaultCenter] postNotificationName:@'ChangeNameNotification' object:self userInfo:@{@'name':self.nameTextField.text}];05.[self dismissViewControllerAnimated:YES completion:nil];06.}else{07.[self showAlert];08.}09.}在A页面的控制器中,注册通知:
1.//RootViewController.m2.- (void)viewDidLoad3.{4.[super viewDidLoad];5.// Do any additional setup after loading the view from its nib.6.[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeNameNotification:) name:@'ChangeNameNotification' object:nil];7.}当我们不使用时,要记得删掉通知:
1.//RootViewController.m2.-(void)dealloc{3.[[NSNotificationCenter defaultCenter] removeObserver:self];4.}调用,显示
1.//RootViewController.m2. 3.-(void)ChangeNameNotification:(NSNotification*)notification{4.NSDictionary *nameDictionary = [notification userInfo];5.self.nameLabel.text = [nameDictionary objectForKey:@'name'];6.}(3)block方式实现
block介绍:http://blog.csdn.net/totogo2010/article/details/7839061
链接一篇描述block回调挺有意思的文章: http://blog.csdn.net/mobanchengshuang/article/details/11751671
分析:
在B试图控制器中,定义一个block,参数为字符串
1.//SecondViewController.h2.typedef void (^ablock)(NSString *str);1.//SecondViewController.h2. 3.@property (nonatomic, copy) ablock block;在B试图控制器中,当输入名字,点击对应的确定按钮后
01.- (IBAction)blockMethod:(id)sender {02.if ([self notEmpty]) {03.if (self.block) {04.self.block(self.nameTextField.text);05.[self dismissViewControllerAnimated:YES completion:nil];06.}07.}else{08.[self showAlert];09.}10.}在A试图显示,回调block
1.- (IBAction)showSecondWithBlock:(id)sender {2.SecondViewController *second = [[SecondViewController alloc] initWithNibName:@'SecondViewController' bundle:nil];3.[self presentViewController:second animated:YES completion:nil];4.second.block = ^(NSString *str){5.self.nameLabel.text = str;6.};7.}在查阅资料的过程中,我还看到了以下几种方案:
(1)使用SharedApplication,定义一个变量来传递(感觉和单例的方式一样)
(2)使用文件,或者NSUserdefault来传递
01.//通过文件或者UserDefault方式存值(感觉不太适合此类传值,如果要用文件或者UserDefault方式存值的话,可以考虑此方式)02.- (IBAction)userDefaultMethod:(id)sender {03.if ([self notEmpty]) {04.[[NSUserDefaults standardUserDefaults] setObject:self.nameTextField.text forKey:@'myNameText'];05.[self dismissViewControllerAnimated:YES completion:nil];06.}else{07.[self showAlert];08.}09.}在A试图控制器显示
01.-(void)viewDidAppear:(BOOL)animated{02.[super viewDidAppear:animated];03.//如果想测试通过UserDefault方式传值或者通过单例方式传值,取消以下注释即可04./*05.if ([[[NSUserDefaults standardUserDefaults] objectForKey:@'myNameText'] length] != 0) {06.self.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@'myNameText'];07.[[NSUserDefaults standardUserDefaults] setObject:@'' forKey:@'myNameText'];08.}09.DataSource *dataSource = [DataSource sharedDataSource];10.if ([dataSource.myName length] != 0) {11.self.nameLabel.text = dataSource.myName;12.dataSource.myName = @'';13.}14.*/15.}(3)通过一个单例的class来传递
B试图控制器
01.//通过单例方式传值(感觉不太适合此类传值,如果要用单例方式传值的话,可以考虑此方式)02.- (IBAction)singletonMethod:(id)sender {03.if ([self notEmpty]) {04.DataSource *dataSource = [DataSource sharedDataSource];05.dataSource.myName = self.nameTextField.text;06.[self dismissViewControllerAnimated:YES completion:nil];07.}else{08.[self showAlert];09.}10.}A试图控制器显示
01.-(void)viewDidAppear:(BOOL)animated{02.[super viewDidAppear:animated];03.//如果想测试通过UserDefault方式传值或者通过单例方式传值,取消以下注释即可04./*05.if ([[[NSUserDefaults standardUserDefaults] objectForKey:@'myNameText'] length] != 0) {06.self.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@'myNameText'];07.[[NSUserDefaults standardUserDefaults] setObject:@'' forKey:@'myNameText'];08.}09.DataSource *dataSource = [DataSource sharedDataSource];10.if ([dataSource.myName length] != 0) {11.self.nameLabel.text = dataSource.myName;12.dataSource.myName = @'';13.}14.*/15.}16.@end这里面用到了单例模式,编写了DataSource这个类,存放数据
01.//02.// DataSource.h03.// TestCallBack04.//05.// Created by csdc-iMac on 14-7-17.06.// Copyright (c) 2014年 JuneWang. All rights reserved.07.//08. 09.#import <Foundation/Foundation.h>10. 11.@interface DataSource : NSObject12.@property (nonatomic, strong) NSString *myName;13.+(DataSource*)sharedDataSource;14.@end 01.//02.// DataSource.m03.// TestCallBack04.//05.// Created by csdc-iMac on 14-7-17.06.// Copyright (c) 2014年 JuneWang. All rights reserved.07.//08. 09.#import 'DataSource.h'10. 11.@implementation DataSource12.+(DataSource *)sharedDataSource{13.static DataSource *dataSource = nil;14.static dispatch_once_t once;15.dispatch_once(&once, ^{16.dataSource = [DataSource new];17.});18.return dataSource;19.}20.@end程序运行截图
A视图:

B视图

当输入姓名,并点击对应的确认按钮后,会回到A视图,并显示在B视图中输入的姓名

iOS页面传值-wang的更多相关文章
- iOS页面传值方式
普遍传值方式如下: 1.委托delegate方式: 2.通知notification方式: 3.block方式: 4.UserDefault或者文件方式: 5.单例模式方式: 6.通过设置属性,实现页 ...
- ios 页面传值4种方式(一) 之全局变量
通用的是用代理的方式实现页面传值,但是有时候利用其它方式也可以很巧妙的解决问题,页面传值一共有4种方式: 1.使用全局变量, SharedApplication,定义一个变量来传递. 2.使用文件,或 ...
- ios页面传值的几种方法
1.属性2.方法3.代理方法4.SharedApplication5.NSUserdefault6.通过一个单例的class来传递 属性这种方法传值挺方便的,只需要拿到它的指针,如果重新声明一个指针, ...
- ios 页面传值4种方式(四) 之通过delegate(代理)
这是ios里最常用的设计模式了,简直贯穿了整个cocoa touch框架.废话不多说,直接上代码: 场景是: A--打开--B; B里输入数值,点击--返回--A; A里显示B输入的值; △在开始写之 ...
- ios常见的页面传值方式
iOS页面间的传值细分有很多种,基本的传值方式有三种:委托Delegate传值.通知NSNotification传值.Block传值,其他在项目中可能会遇到的还有:UserDefault或文件方式传值 ...
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值实现方法:1.通过设置属性,实现页面间传值:2.委托delegate方式:3.通知notification方式:4.block方式:5.UserDefault或者文件方式:6.单例模式 ...
- iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...
- 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错
原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...
- iOS 页面间传值 之 单例传值 , block 传值
ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...
随机推荐
- Bootstrap学习笔记(二)
这一节笔记主要记录排版内容笔记,其内容包括标题.文本(包括段落.粗斜体.对齐).列表.表格等. 一.标题 在bootstrap中H1-H6与非框架版的区别不大,需要注意的是<small>标 ...
- eclipse 启动失败(找不到jvm)
今天启动eclipse时提示了一个错误 在网上找到的解决方法是在eclipse的快捷方式中加入Java的JVM的路径,方法如下: 右键eclipse快捷方式 ->属性 在目标中 如果只有 D: ...
- android 混淆导致友盟反馈出错
最近混淆应用时出现了一个错误: 打包编译没问题,但程序运行之后,点击友盟的反馈就会导致程序报错: E/AndroidRuntime(7669): java.lang.RuntimeException ...
- 使用Crowd2.7集成Confluence5.3与JIRA6.1,并安装、破解及汉化,实现单点登录【原创】
鉴于目前没有针对Crowd.Confluence.Jira安装.集成和破解最新的方法,总结今天安装.破解及集成的经验,编写此文,方便大家进行配置也方便自己以后参考.此文参考多篇破解文章,并经过作者 ...
- Devexpress XtraReport 打印时弹出Margins提示解决办法
当我们用Dev的报表引擎做报表时,如果把边缘设置为0时会弹出提示. 可以通过代码 XtraReport.PrintingSystem.ShowMarginsWarning = false; 取消该提示
- php web系统多域名登录失败解决方法
下面只是简单的逻辑结构,对于正式的系统需要做具体的处理. 这里需要注意的是:加解密一定需要做安全验证.但是这个方法也不够完美,两个站点必须有相同一级域名:另外这种完全基于cookie的方式,安全性不够 ...
- Android Studio2.2.2下RecyclerView的使用
1,概述 RecyclerView可以完全代替ListView.GridView,整体上看RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同Layout ...
- android优秀Github源码整理
1.https://github.com/sd6352051/NiftyNotification 2.https://github.com/sd6352051/NiftyDialogEffects 3 ...
- 用springMVC构建restful程序,接收以及返回json数据格式
主要参考文章:http://kingxss.iteye.com/blog/1487745和http://blog.csdn.net/greensurfer/article/details/192962 ...
- snoopy采集
Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单.Snoopy正确运行需要你的服务器的PHP版本在4以上,并且支持PCRE(Perl Compatible Regular ...