在项目中,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之间传递数据:属性传值的更多相关文章

  1. 【MVC架构】——怎样利用Json在View和Controller之间传递数据

    在MVC架构中,尽管非常多东西和三层非常相似,可是也有非常大的差别.就比方传递数据.在三层架构中,传递数据就仅仅要一层返回,另外一层用同样类型的变量来接收即可了.在MVC中,事实上原理是一样的,Con ...

  2. Controller之间传递数据:Block传值

    http://itjoy.org/?p=420 前边我们介绍过属性传值和协议传值,这里介绍一下块传值,块类似于C中的函数指针.在Controller中传递数据非常方便,还是继续上一章的例子,将数据从S ...

  3. Controller之间传递数据:协议传值

    http://itjoy.org/?p=416 前边介绍过从第一个页面传递数据到第二个页面,那么反过来呢我们该如何操作?还是同一个例子,将第二个页面的字符串传递到第一个页面显示出来,这中形式就可以使用 ...

  4. 【ASP.NET MVC】View与Controller之间传递数据

    1   概述 本篇文章主要从操作上简要分析Controller<=>View之间相互传值,关于页面之间传值,如果感兴趣,可参考我另外一篇文章ASP.NET 页面之间传值的几种方式 . Co ...

  5. Activity之间传递数据的方式及常见问题总结

    Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...

  6. Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口

    package com.gaojinhua.android.activitymsg; import android.content.Intent; import android.os.Bundle; ...

  7. 28、activity之间传递数据&批量传递数据

    import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...

  8. 【Android 复习】 : Activity之间传递数据的几种方式

    在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...

  9. Android基础知识04—Activity活动之间传递数据

    ------活动之间传递数据------ 向下一个活动传递数据: Intent中提供了一系列的putExtra()方法,可以把数据暂存到Intent中,启动另一个活动的时候就可以取出来. 代码: (存 ...

随机推荐

  1. JS所谓的享元模式-->

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  2. hdu5007 字符串

    字符串问题.是否出现iPhone Apple等词:我考虑时想到既然是否有这些词,可以写map标记一下:然后又最长的是iPhone,6个单词,所以第一个for遍历所有单词 然后在一个for(1~6),用 ...

  3. UVA 540 stl

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

  4. WeakHashMap和HashMap的区别

    看Java源码的时候,看到了 WeakHashMap ,我一直以来使用的 都是 HashMap,于是查了一下两者的区别 (一) 查看API文档,WeakHashmap要点如下: 1. 以弱键 实现的基 ...

  5. js-定时任务setInterval,setTimeout,clearInterval,clearTimeout

    setInterval()循环执行相应的方法 <script type="text/javascript"> setInterval("myInterval( ...

  6. 洛谷P1508 Likecloud-吃、吃、吃

    题目背景 问世间,青春期为何物? 答曰:“甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏 ...

  7. C#获取本机的MAC地址

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.M ...

  8. chroot详解

    我是一个刚接触 Linux 和 Unix 的新手.我该如何改变一个命令的根目录?我要怎样改变一个进程的根目录呢,比如用 chroot 命令将web服务与文件系统隔离?我要如何使用 chroot 恢复密 ...

  9. 彻底理解position与anchorPoint

    引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与positio ...

  10. MongoDB的安装 转

    第1章 MongoDB的安装 (黎明你好原创作品,转载请注明) 1.1 MongoDB简介 MongoDB是一个基于分布式文件存储的数据库开源项目.由C++语言编写,旨在为WEB应用提供可护展的高性能 ...