http://itjoy.org/?p=420

前边我们介绍过属性传值和协议传值,这里介绍一下块传值,块类似于C中的函数指针。在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
///////////
////////FirstViewController
- (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.send = ^(NSString *str){
        self.nameLable.text = str;
    };
    //推过去
    [self.navigationController pushViewController:second animated:YES];
    [second release];
}
 
 
 
 
 

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
/////////////
////////////SecondViewController.h
#import <UIKit/UIKit.h>
typedef  void (^SendMessage) (NSString *str); ///声明块
 
@interface SecondViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic, copy) SendMessage send;  //声明一个块类型属性
@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];
    //先判断,在调用块传递实参
    if (self.send) {
        self.send (textField.text);
    }
    return YES;
}
 

Controller之间传递数据:Block传值的更多相关文章

  1. Controller之间传递数据:属性传值

    在项目中,Controller之间传递数据非常之多,这里简单介绍一下属性传值.例如有FirstController 和 SecondController,数据从First传递到Second中,我们如何 ...

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

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

  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. AJAX(二)AJAX框架

    上文(AJAX(一)AJAX的简介和基础)对ajax异步请求服务器做了详细的介绍和基础应用,可以看出,ajax的一些过程是相对不变的.不必要每次发送请求都写一遍发送代码,一些ajax开发人员已经把他们 ...

  2. WEB前端开发规范

    WEB前端开发规范 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档.本文档如有不对或者不合适的地方请及时提出, 经讨论决定后方可更改. 基本 ...

  3. .net 使用PowerShell获取电脑中的UUID

    UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OS ...

  4. sql- 别名alias(as)

    alias (别名) 在 SQL 上的用处.最常用到的别名有两种: 栏位别名及表格别名. 简单地来说,栏位别名的目的是为了让 SQL 产生的结果易读.在之前的例子中,每当我们有营业额总合时,栏位名都是 ...

  5. url的编码问题

    JQuery中 编码 var url = 'folder/index.html?param=#23dd&noob=yes'; var encodedUrl = encodeURICompone ...

  6. 洛谷P1174 打砖块

    题目描述 小红很喜欢玩一个叫打砖块的游戏,这个游戏的规则如下: 在刚开始的时候,有n行*m列的砖块,小红有k发子弹.小红每次可以用一发子弹,打碎某一列当前处于这一列最下面的那块砖,并且得到相应的得分. ...

  7. Java NIO、NIO.2学习笔记

    相关学习资料 http://www.molotang.com/articles/903.html http://www.ibm.com/developerworks/cn/education/java ...

  8. hdu acmsteps 2.2.1 Fibonacci

    Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  9. jquery ajax 应用返回类型是html json

    jquery ajax 例子:    function JudgeUserName()        {            $.ajax({            type:"GET&q ...

  10. BUAA1389愤怒的DZY(最大值最小化)

    http://acm.buaa.edu.cn/problem/1389/ 愤怒的DZY[问题描述]“愤怒的小鸟”如今已经是家喻户晓的游戏了,机智的WJC最近发明了一个类似的新游戏:“愤怒的DZY”.游 ...