在网上看了看传值方法,没有找到完整的.在这把自己看到的几种传值方法写写吧.

1 . 属性传值

2 . 通知传值

3 . 代理传值

4 . block传值

5 . 单列传值

6 . ShareApplication

7 . NSUserdefault

1 . 属性传值

我个人理解就是在页面发生跳转的时候把本页面的值传递到目标页面.

2 . 通知传值

在A页面发布通知,在需要数据的页面监听A所发布的通知, 监听结束,移除监听.

3 代理传值

自己对这个不是很了解

RootViewControllers页面push到DetailViewControllers页面,如果DetailViewControllers页面的信息想回传(回调)到RootViewControllers页面,用代理传值,其中DetailViewControllers定义协议和声明代理,RootViewControllers确认并实现代理,RootViewControllers作为DetailViewControllers的代理

首先在DetailViewControllers.h文件中我们创建协议方法

在DetailViewControllers的.m中我们判定代理对象存在时,为其绑定相应方法

RootViewControllers的.m文件中我们指定代理并让其执行代理的方法

4.block传值

Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值。

5 . 单列传值

AppStatus.h  创建一个单例类 AppStatus

 1 #import <Foundation/Foundation.h>
2
3 @interface AppStatus : NSObject
4 {
5 NSString *_contextStr;
6 }
7
8 @property(nonatomic,retain)NSString *contextStr;
9
10 +(AppStatus *)shareInstance;
11
12 @end AppStatus.m
1 #import "AppStatus.h"
2
3 @implementation AppStatus
4
5 @synthesize contextStr = _contextStr;
6
7 static AppStatus *_instance = nil;
8
9 +(AppStatus *)shareInstance
10 {
11 if (_instance == nil)
12 {
13 _instance = [[super alloc]init];
14 }
15 return _instance;
16 }
17
18 -(id)init
19 {
20 if (self = [super init])
21 {
22
23 }
24 return self;
25 }
26
27 -(void)dealloc
28 {
29 [super dealloc];
30 }
31
32 @end

RootViewController.h

1 #import "RootViewController.h"
2 #import "DetailViewController.h"
3 #import "AppStatus.h"
4
5 @interface RootViewController ()
6
7 @end
8
9 @implementation RootViewController
10
11 -(void)loadView
12 {
13 //核心代码
14 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
15 btn.frame = CGRectMake(0, 0, 100, 30);
16 [btn setTitle:@"Push" forState:0];
17 [btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
18 [self.view addSubview:btn];
19 }
20
21 -(void)pushAction:(id)sender
22 {
23 tf = (UITextField *)[self.view viewWithTag:1000];
24
25 //单例传值 将要传递的信息存入单例中(共享中)
26 // [[AppStatus shareInstance]setContextStr:tf.text]; 跟下面这种写法是等价的
27 [AppStatus shareInstance].contextStr = tf.text;
28 //导航push到下一个页面
29 //pushViewController 入栈引用计数+1,且控制权归系统
30 DetailViewController *detailViewController = [[DetailViewController alloc]init];
31
32 //导航push到下一个页面
33 [self.navigationController pushViewController:detailViewController animated:YES];
34 [detailViewController release];
35 }
36
37 @end

DetailViewController.h

1 #import <UIKit/UIKit.h>
2 @protocol ChangeDelegate;//通知编译器有此代理
3
4 @interface DetailViewController : UIViewController
5 {
6 UITextField *textField;
7 }
8
9 @end

DetailViewController.m

 1 #import "DetailViewController.h"
2 #import "AppStatus.h"
3
4 @interface DetailViewController ()
5
6 @end
7
8 @implementation DetailViewController
9
10 @synthesize naviTitle = _naviTitle;
11
12 -(void)loadView
13 {
14 self.view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];
15
16 //单例
17 self.title = [AppStatus shareInstance].contextStr;
18 textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
19 textField.borderStyle = UITextBorderStyleLine;
20 [self.view addSubview:textField];
21 [textField release];
22
23 UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)];
24 self.navigationItem.rightBarButtonItem = doneItem;
25 [doneItem release];
26 }
27
28 //这个方法是执行多遍的 相当于刷新view
29 -(void)viewWillAppear:(BOOL)animated
30 {
31 [super viewWillAppear:animated];
32 tf = (UITextField *)[self.view viewWithTag:1000];
33 tf.text = [AppStatus shareInstance].contextStr;
34 }
35
36 //pop回前一个页面
37 -(void)doneAction:(id)sender
38 {
39 //单例传值
40 [AppStatus shareInstance].contextStr = textField.text;
41 [self.navigationController popToRootViewControllerAnimated:YES];
42 }

6 .ShareApplication

对于这种传值方式不怎么理解

7.NSUserdefault

我自己的NSUserdefault的理解就是用本数据暂时存储到本地,然后在其他页面需要的时候在获取.

ios 多种传值方式的更多相关文章

  1. iOS页面传值方式

    普遍传值方式如下: 1.委托delegate方式: 2.通知notification方式: 3.block方式: 4.UserDefault或者文件方式: 5.单例模式方式: 6.通过设置属性,实现页 ...

  2. iOS 各种传值方式

    属性传值 将A页面所拥有的信息通过属性传递到B页面使用 B页面定义了一个naviTitle属性,在A页面中直接通过属性赋值将A页面中的值传到B页面. A页面DetailViewController.h ...

  3. iOS学习之六种传值方式

    iOS页面传值方式 应用于: 两个互动的界面:1)页面一跳转到页面二,页面一的textField的值传给页面二的label.2)A页面跳转到B页面,B页面再跳转回A页面(注册页面跟登录页面) 两个不互 ...

  4. ios常见的页面传值方式

    iOS页面间的传值细分有很多种,基本的传值方式有三种:委托Delegate传值.通知NSNotification传值.Block传值,其他在项目中可能会遇到的还有:UserDefault或文件方式传值 ...

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

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

  6. iOS学习——页面的传值方式

    一.简述 在iOS开发过程中,页面跳转时在页面之间进行数据传递是很常见的事情,我们称这个过程为页面传值.页面跳转过程中,从主页面跳转到子页面的数据传递称之为正向传值:反之,从子页面返回主页面时的数据传 ...

  7. iOS 浅谈MVC设计模式及Controllers之间的传值方式

    1.简述你对MVC的理解? MVC是一种架构设计.它考虑了三种对象:Model(模型对象).View(试图对象).Controller(试图控制器) (1)模型:负责存储.定义.操作数据 (2)视图: ...

  8. iOS 页面间几种传值方式(属性,代理,block,单例,通知)

    第二个视图控制器如何获取第一个视图控制器的部分信息 例如 :第二个界面中的lable显示第一个界面textField中的文本 这就需要用到属性传值.block传值 那么第一个视图控制器如何获的第二个视 ...

  9. iOS的四种传值方式

    传值有四种方法 : 1. 属性传值 2. 单例传值 3. 代理传值 4. block传值     一.属性传值   (前-->后) 1. 后面的界面定义一个属性  存放前一个界面传过来的值    ...

随机推荐

  1. Bootstrap学习笔记(二)

    这一节笔记主要记录排版内容笔记,其内容包括标题.文本(包括段落.粗斜体.对齐).列表.表格等. 一.标题 在bootstrap中H1-H6与非框架版的区别不大,需要注意的是<small>标 ...

  2. android 取消edittext焦点

    页面中如果有EditText会默认获取焦点,如果进入页面时不想让其获取到焦点可以按如下步骤: 1.在布局的最外层添加属性: android:focusable="true" and ...

  3. codeblocks个性化配置

    1.general setting设置默认字体大小设置控制台字体大小:"Settings -> Environment -> View -> Message logs' f ...

  4. map的四种遍历方式

    map是Java中非常常用的一种数据结构,但map不同于set和list都继承自Collection接口. 所以map没有实现Collection的Iterator 方法,自身没有迭代器来遍历元素. ...

  5. UDP丢包严重

    项目要求udp能够达到10万的并发量,搞了几天,丢包严重, 今天终于解决了,原来是socket缓冲区设置的不够大已经jvm内存不够大

  6. php类的__get和__set方法

    (1)这两个方法是自动调用的 (2)这两个方法只有在成员变量是private的时候才会自己调用 testclass.php <?php class testclass { private $va ...

  7. 不使用ASP.NET中的服务器控件将如何上传文件?

    遇到文件的上传时,可能会有大部分的开发者喜欢使用服务器控件,虽然很方便,但是却不能很好的控制,不具灵活性. 现给出例子,使用html标签语言灵活的控制文件的上传. 1.html部分 <input ...

  8. jquery如何根据text选择option

    百度出来的代码都是这样的: $('#test option[text="b"]').attr("selected",true); 或 $('#test').fi ...

  9. 对于java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I错误解决

    在J2EE框架下开发web网站,这种问题经常遇到,只要我们网上搜一下,就可以看到很多版本的,我整理一下: 第一种可能性解决:看看我的项目:主要 是里面的Structs 1.3 (structs 2)和 ...

  10. 好久没写Blog了

    上一年的经历: <炸年兽>搞了一阵后,美术去创业了.. 和另一个美术断断续续,做了个<斗战圣佛>,挺山寨的,都没敢跟别人说. 不管怎么说也算是自己上了一个appStore的游戏 ...