iOS传值方式:属性,代理,block,单例,通知
正向传值均可,反向传值除属性传值不可,其余均可。下面简单介绍:
(一)属性传值
第二个界面中的lable显示第一个界面textField中的文本
首先我们建立一个RootViewControllers和一个DetailViewControllers,在DetailViewControllers中声明一个textString属性,用于接收传过来的字符串,
同时创建一个Lable用来显示传过的字符串
在RootViewControllers上引入DetailViewControllers同时声明一个textField属性用来输入字符串
然后在RootViewControllers上我们创建并添加一个button,当点击button时响应相应方法进行视图间的切换完成视图间的传值
(二)Block传值
block传值也是从第一个界面给第二个界面传值
首先我们在DetailViewcontrollers的.h文件中,属性
在RootViewControllers的.m文件中,其他不变,在button的响应方法里我们为block属性赋值完成block传值
(三)代理传值
RootViewControllers页面push到DetailViewControllers页面,如果DetailViewControllers页面的信息想回传(回调)到RootViewControllers页面,用代理传值,其中DetailViewControllers定义协议和声明代理,RootViewControllers确认并实现代理,RootViewControllers作为DetailViewControllers的代理
首先在DetailViewControllers.h文件中我们创建协议方法
在DetailViewControllers的.m中我们判定代理对象存在时,为其绑定相应方法
RootViewControllers的.m文件中我们指定代理并让其执行代理的方法
(四)单例传值
单例传值(实现共享)
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 }

(五)通知传值
谁要监听值的变化,谁就注册通知 特别要注意,通知的接受者必须存在这一先决条件
A页面RootViewController.h

1 #import <UIKit/UIKit.h>
2 #import "DetailViewController.h"
3 @interface RootViewController : UIViewController<ChangeDelegate>
4 {
5 UITextField *tf;
6 }
7 @end

A页面RootViewController.m

1 #import "IndexViewController.h"
2 #import "DetailViewController.h"
3 #import "AppStatus.h"
4
5 @implementation IndexViewController
6
7 -(void)dealloc
8 {
9 [[NSNotificationCenter defaultCenter] removeObserver:self
10 name:@"CHANGE_TITLE" object:nil];
11 [super dealloc];
12 }
13
14 -(id)init
15 {
16 if (self = [super init])
17 {
18 [[NSNotificationCenter defaultCenter] addObserver:self
19 selector:@selector(change:)
20 name:@"CHANGE_TITLE"
21 object:nil];
22 }
23 return self;
24 }
25
26 -(void)change:(NSNotification *)aNoti
27 {
28 // 通知传值
29 NSDictionary *dic = [aNoti userInfo];
30 NSString *str = [dic valueForKey:@"Info"];
31
32 UITextField *tf = (UITextField *)[self.view viewWithTag:1000];
33 tf.text = str;
34 }
35
36 -(void)viewWillAppear:(BOOL)animated
37 {
38 [super viewWillAppear:animated];
39 /*
40 // 单例传值
41 UITextField *tf = (UITextField *)[self.view viewWithTag:1000];
42 tf.text = [AppStatus shareInstance].contextStr;
43 */
44 }
45
46 @end

DetailViewController.h

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

DetailViewController.m

1 #import "DetailViewController.h"
2 #import "AppStatus.h"
3
4 @implementation DetailViewController
5 @synthesize naviTitle = _naviTitle;
6
7 -(void)loadView
8 {
9 UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)];
10 self.navigationItem.rightBarButtonItem = doneItem;
11 [doneItem release];
12 }
13
14 // pop回前一个页面
15 -(void)doneAction:(id)sender
16 {
17 NSDictionary *dic = [NSDictionary dictionaryWithObject:textField.text forKey:@"Info"];
18
19 [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_TITLE" object:nil userInfo:dic];
20
21 [self.navigationController popViewControllerAnimated:YES];
22
23 }
iOS传值方式:属性,代理,block,单例,通知的更多相关文章
- iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...
- 传值 属性 block 单例 协议
界面传值 四种传值的方式 1.属性传值(从前往后) 步骤: 1.属性传值用于第一个界面向第二个界面传值 2.明确二者联系的桥梁,也就是触发跳转的地方 3.明确传输的值,类型是什么 4.在第二个视图控制 ...
- iOS常用设计模式:MVC、单例、代理、观察者。
MVC 模型-视图-控制器(MVC)设计模式 MVC根据角色划分类,涉及到三个角色: Model:模型保存应用程序的数据. View:视图是模型的可视化表示以及用户交互的控件. Controller: ...
- Python中的私有属性私有方法、类属性类方法以及单例设计模式
私有属性是对象不希望公开的属性,私有方法是对象不希望公开的方法.在定义私有属性和私有方法时,在属性或者方法前,加上__(两个下划线) 公有方法可以通过对象名直接调用,私有方法不能通过对象名直接调用,只 ...
- iOS 用宏定义写一个单例(Singleton)
用如下方法定义单例 @interface singleton_interface(ClassName); @end 实现单例在 @implemention singleton_implemention ...
- java新手笔记11 类的静态属性、方法(单例)
1.Person类 package com.yfs.javase; public class Person { String name;//每个对象上分配 与对象绑定 int age; char se ...
- IOS设计模式学习(7)单例
1 前言 数学与逻辑学中,singleton定义为“有且仅有一个元素的集合”.因此不管袋子有多大,每次从里面取出弹子的时候,拿到的都是同一个. 2 详述 2.1 简述 面向对象应用程序中的单例类(si ...
- iOS 传值方式
1.代理传值 2.AppDelegate传值 3.block传值 4.通知传值 5.NSUserDefault传值
- iOS 传值 委托(delegate)和block 对比
技术交流新QQ群:414971585 这篇文章建议和前一篇一起看, 另外先弄清楚IOS的block是神马东东. 委托和block是IOS上实现回调的两种机制.Block基本可以代替委托的功能,而且实 ...
随机推荐
- Trapping Rain Water [LeetCode]
Problem Description: http://oj.leetcode.com/problems/trapping-rain-water/ Basic idea: Get the index ...
- poj1258 Agri-Net (prim+heap)
题目链接:poj1258 Agri-Net 这题我上个月做过,是个大水题,今天看见有人用prim+heap做的,就学习了下. #include<cstdio> #include<cs ...
- python的一点小常识
在Python中,有两种变长参数,分别是元组(非关键字参数)和字典(关键字参数),其参数以一个*开头表示任意长度的元组[tuple],可以接收连续一串参数,参数以两个*开头表示一个字典[dict],即 ...
- 项目管理办公室 PMO
项目管理办公室是组织中指导,协调,支持项目管理工作的一个常设职能部门,也就是管理项目管理的常设职能部门. 它负责指定和贯彻标准化的项目管理方法论(包括工作流程与规章制度等),协调所辖的各项目对资源,工 ...
- 如何删除href=""中的链接?
答案:在dw中操作,删除 HTML文件的href的链接地址\href="[^"]*"href="" 同理可以在title="[^" ...
- 目前几款基于html5的前端框架:如Bootstrap、Foundation、Semantic UI 、Amaze UI
Bootstrap是由Twitter在2011年8月推出的开源WEB前端框架,集合CSS 和HTML,使用了最新的浏览器技术,为快速WEB开发提供了一套前端工具包,包括布局.网格.表格.按钮.表单.导 ...
- OkHttp使用全解析(转)。
Android系统提供了两种HTTP通信类,HttpURLConnection和HttpClient.关于HttpURLConnection和HttpClient的选择>>官方博客尽管Go ...
- JQuery判断checkbox是否选中-批量
在html的checkbox里,选中的话会有属性checked="checked". 如果用一个checkbox被选中,alert这个checkbox的属性"checke ...
- C#获取本机mac地址
添加System.Management的引用, using System.Management; string mac = ""; ManagementClass mc = new ...
- 戴文的Linux内核专题:08内核配置(5)
转自Linux中国 Linux内核拥有许多可以配置的特性,接下来我们还有许多要配置. 下一个可以配置的特性是x86的随机数生成器(x86 architectural random number gen ...