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. 每天一个linux命令(23): chown命令

    chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...

  2. Node on Mac 初步

    安装node之前 1.之前在windows平台上安装node,直接下载一个安装文件得了.但是mac osx是基于unix架构的嘛,跟linux很像啊. 在linux上安装我们多习惯用命令行工具安装啊, ...

  3. onethink常用标签的使用示例

    首页文章模型列表输出: <article:list name="article" category="2" row="3" order ...

  4. UVA5874 Social Holidaying 二分匹配

    二分匹配简单题,看懂题意,建图比较重要. #include<stdio.h> #include<string.h> #define maxn 1100 int map[maxn ...

  5. 最大ASCII的和问题

    问题:One day when you are going to clear all your browsing history, you come up with an idea: You want ...

  6. [转] 计算几何模板Orz

    #include<math.h> #define MAXN 1000 #define offset 10000 #define eps 1e-8 #define PI acos(-1.0) ...

  7. Server Data Synchronization Via Linux rsync、rsync+inotify Between Load Balance Server

    目录 . 远程文件同步的应用场景 . rsync+crontab . rsync+inotify 1. 远程文件同步的应用场景 在负载均衡集群的应用场景中,往往在多台web server的前端有一个提 ...

  8. Linux的学习路线图

    一.学习Linux的基本要求1. 掌握至少50个以上的常用命令. 2. 熟悉Gnome/KDE等X-windows桌面环境操作 . 3. 掌握.tgz..rpm等软件包的常用安装方法 4. 学习添加外 ...

  9. idea修改运行内存

    安装目录下的bin 找到idea.exe.vmoptions 最大的修改下-Xmx1024m 找到idea64.exe.vmoptions 最大的修改下-Xmx1024m

  10. spark对于elasticsearch里的复杂类型支持

    IP,直接在case class里用string, 可以考虑先用其它程序生成相关的mapping,然后再去用spark填充数据