Controller之间传递数据:属性传值
在项目中,Controller之间传递数据非常之多,这里简单介绍一下属性传值。例如有FirstController 和 SecondController,数据从First传递到Second中,我们如何操作呢,比如我们传递一个字符串到Second,那么我们就可以在Second中创建一个属性,在First中,推向Second的时候,为Second中那个属性赋值即可。
代码如下所示:
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
|
/////////////////FirstViewController//////////////////
- (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];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(130, 170, 60, 40);
[button setTitle:@"传值" forState:UIControlStateNormal];
[button addTarget:self action:@selector(sendValue:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)sendValue:(id)sender
{
//获取textfd里面的数据
UITextField *textfield = (UITextField *)[self.view viewWithTag:100];
//初始化第二个second
SecondViewController *second = [[SecondViewController alloc]init];
//为second中的name属性赋值
second.name = textfield.text;
//推过去
[self.navigationController pushViewController:second animated:YES];
[second release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
|
Objective-C
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
////////////////////SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *nameLable = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 60)];
nameLable.textAlignment = UITextAlignmentCenter;
nameLable.font = [UIFont systemFontOfSize:50];
nameLable.textColor = [UIColor blueColor];
//将属性的值赋值给这个label
nameLable.text = self.name;
[self.view addSubview:nameLable];
[nameLable release];
}
|
Controller之间传递数据:属性传值的更多相关文章
- 【MVC架构】——怎样利用Json在View和Controller之间传递数据
在MVC架构中,尽管非常多东西和三层非常相似,可是也有非常大的差别.就比方传递数据.在三层架构中,传递数据就仅仅要一层返回,另外一层用同样类型的变量来接收即可了.在MVC中,事实上原理是一样的,Con ...
- Controller之间传递数据:Block传值
http://itjoy.org/?p=420 前边我们介绍过属性传值和协议传值,这里介绍一下块传值,块类似于C中的函数指针.在Controller中传递数据非常方便,还是继续上一章的例子,将数据从S ...
- Controller之间传递数据:协议传值
http://itjoy.org/?p=416 前边介绍过从第一个页面传递数据到第二个页面,那么反过来呢我们该如何操作?还是同一个例子,将第二个页面的字符串传递到第一个页面显示出来,这中形式就可以使用 ...
- 【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中,启动另一个活动的时候就可以取出来. 代码: (存 ...
随机推荐
- php empty()和isset()的区别
在使用 php 编写页面程序时,我经常使用变量处理函数判断 php 页面尾部参数的某个变量值是否为空,开始的时候我习惯了使用 empty() 函数,却发现了一些问题,因此改用 isset() 函数,问 ...
- 【HDU 5399】Too Simple
题 Description Rhason Cheung had a simple problem, and asked Teacher Mai for help. But Teacher Mai th ...
- 淘宝业务常用english
ADX ad exchange 广告交易平台 coupon 赠品 CPC cost per click CPS cost per sales CT ...
- 和声搜索算法-python实现
HSIndividual.py import numpy as np import ObjFunction class HSIndividual: ''' individual of harmony ...
- @RestController注解下返回到jsp视图页面(转)(转)
这个问题我也遇到过,下面的方法可以试试 蓝萝卜blu @RestController注解下返回到jsp视图页面 spring4.1中添加了@RestController注解很方便,集成了@Respon ...
- SpringMVC中Controller和RestController
项目中的@Controller下有的是返回String类型的(比如getAllBook),有的是void的,当然,String类型是转发的页面,在void中用的是pringwrite,我今天想做一件事 ...
- groovy-保留字
groovy的保留字: abstractasassertbooleanbreakbytecasecatchcharclassconstcontinuedefdefaultdodoubleelseenu ...
- Codeforces 295A Greg and Array
传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...
- C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试(续)
介绍 本文是接着上文<C# WPF 显示图片和视频显示 EmuguCv.AForge.Net测试>写的,建议先看下上文,因为有些代码还需要了解. 增添 接着上文的代码,我们可以在事件处理方 ...
- AppStore占坑注意事项
AppStore占坑注意事项 我们会提前在AppStore(iTunesConnect)里注册一些应用名称,以满足未来业务需要和防止恶意注册,其中有一些需要注意的事情,整理如下: 倒计时180天 为了 ...