Controller之间传递数据:协议传值
前边介绍过从第一个页面传递数据到第二个页面,那么反过来呢我们该如何操作?还是同一个例子,将第二个页面的字符串传递到第一个页面显示出来,这中形式就可以使用协议来传值,协议我们可以理解成双方规定好一组标准,都满足这个标准我们之间就可以通信,一方通过协议发送数据,另一方通过协议来接受数据。
代码如下:从Second传递数据到First
Objective-C
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
/////////////////////////
/////////FirstViewController.h////////////
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface FirstViewController : UIViewController<UITextFieldDelegate,SendMessage> //遵守SendMessage协议
@property (nonatomic, retain) UILabel *nameLable;
@end
///////////FirstViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.nameLable = [[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 60)]autorelease];
self.nameLable.textAlignment = UITextAlignmentCenter;
self.nameLable.font = [UIFont systemFontOfSize:50];
self.nameLable.textColor = [UIColor blueColor];
[self.view addSubview:self.nameLable];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(130, 170, 60, 40);
[button setTitle:@"下一个" forState:UIControlStateNormal];
[button addTarget:self action:@selector(pushNext:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)pushNext:(id)sender
{
//初始化second
SecondViewController *second = [[SecondViewController alloc]init];
//设置代理,由谁去执行
second.delegate = self;
//推过去
[self.navigationController pushViewController:second animated:YES];
[second release];
}
//实现协议的方法
- (void)sendValue:(NSString *)str
{
//赋值操作
self.nameLable.text = str;
}
|
Objective-C
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
////////////////////////
//////////////////SecondViewController.h
#import <UIKit/UIKit.h>
@protocol SendMessage;
@interface SecondViewController : UIViewController<UITextFieldDelegate>
@property(nonatomic, assign)id<SendMessage> delegate;
@end
///协议的定义,包含一个方法。
@protocol SendMessage <NSObject>
- (void)sendValue:(NSString *)str;
@end
/////////////SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UITextField *textFd = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 300, 150)];
textFd.borderStyle = UITextBorderStyleRoundedRect;
textFd.delegate = self;
textFd.tag = 100;
[self.view addSubview:textFd];
[textFd release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
//如果delegate 是这个类型的就调用这个方法,类似于java中的接口,子类实现接口,可以调用接口中的方法
if ([self.delegate conformsToProtocol:@protocol(SendMessage) ]) {
[self.delegate sendValue:textField.text];
}
return YES;
}
|
Controller之间传递数据:协议传值的更多相关文章
- Controller之间传递数据:属性传值
在项目中,Controller之间传递数据非常之多,这里简单介绍一下属性传值.例如有FirstController 和 SecondController,数据从First传递到Second中,我们如何 ...
- 【MVC架构】——怎样利用Json在View和Controller之间传递数据
在MVC架构中,尽管非常多东西和三层非常相似,可是也有非常大的差别.就比方传递数据.在三层架构中,传递数据就仅仅要一层返回,另外一层用同样类型的变量来接收即可了.在MVC中,事实上原理是一样的,Con ...
- Controller之间传递数据:Block传值
http://itjoy.org/?p=420 前边我们介绍过属性传值和协议传值,这里介绍一下块传值,块类似于C中的函数指针.在Controller中传递数据非常方便,还是继续上一章的例子,将数据从S ...
- 【ASP.NET MVC】View与Controller之间传递数据
1 概述 本篇文章主要从操作上简要分析Controller<=>View之间相互传值,关于页面之间传值,如果感兴趣,可参考我另外一篇文章ASP.NET 页面之间传值的几种方式 . Co ...
- Activity之间传递数据的方式及常见问题总结
Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...
- Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口
package com.gaojinhua.android.activitymsg; import android.content.Intent; import android.os.Bundle; ...
- 28、activity之间传递数据&批量传递数据
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...
- 【Android 复习】 : Activity之间传递数据的几种方式
在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...
- Android基础知识04—Activity活动之间传递数据
------活动之间传递数据------ 向下一个活动传递数据: Intent中提供了一系列的putExtra()方法,可以把数据暂存到Intent中,启动另一个活动的时候就可以取出来. 代码: (存 ...
随机推荐
- JQuery思维导图
- Hibernate-二级缓存策略
二级缓存的策略 当多个并发的事务同时访问持久化层的缓存中的相同数据时,会引起并发问题,必须采用必要的事务隔离措施. 在进程范围或集群范围的缓存,即第二级缓存,会出现并发问题.因此可以设定以下4种类型的 ...
- ActiveRecord中andFilterWhere使用
查询数据库时 $model; if(!empty($name)) { $model->andWhere(['name'=>$name]); } 可以用andFilterWhere,自动的把 ...
- maven加载spring包
<dependencies> <dependency> <groupId>org.springframework</groupId> <artif ...
- Java虚拟机类加载机制
看到这个题目,很多人会觉得我写我的java代码,至于类,JVM爱怎么加载就怎么加载,博主有很长一段时间也是这么认为的.随着编程经验的日积月累,越来越感觉到了解虚拟机相关要领的重要性.闲话不多说,老规矩 ...
- jquery ajax 应用返回类型是html json
jquery ajax 例子: function JudgeUserName() { $.ajax({ type:"GET&q ...
- jQuery的查找
children([expr])概述 :取得一个包含匹配的元素集合中每一个元素的所有子元素的元素集合.可以通过可选的表达式来过滤所匹配的子元素.注意:parents()将查找所有祖辈元素,而child ...
- IOS基础之 (十) 内存管理
一 基本原理 1.什么是内存管理 移动设备的内存有限,每个app所能占用的内存是有限制的. 当app所占用的内存较多时,系统会发出内存警告,这时得回收一些不需要再使用的内存空间.比如回收一些不需要使用 ...
- php多态设计
原文:http://www.cnblogs.com/tecs27/archive/2012/03/13/2394028.html 多态性是指相同的操作或函数.过程可作用于多种类型的对象上并获得不同的结 ...
- MySQL索引的创建、删除和查看
MySQL索引的创建.删除和查看 此文转自http://blogold.chinaunix.net/u3/93470/showart_2001536.html 1.索引作用 在索引列上,除了上面提到的 ...