//
// ZYViewController.h
// BlockTest
//
// Created by yejiong on 14/11/2.
// Copyright © 2014年 zzz. All rights reserved.
// #import <UIKit/UIKit.h> //1.声明一个 delegate 协议。
@protocol ZYViewControllerDelegate <NSObject> //声明一个方法用来传值。
//返回值表明传值的结果,如果不关心那么就写 void。
//方法中的参数就是我们需要传的值,如果要传多个值,后面继续追加参数。
- (void)selectedColor:(UIColor* )color; @end @interface ZYViewController : UIViewController //2.声明一个 delegate 属性。
@property (nonatomic, assign) id<ZYViewControllerDelegate> delegate; @end
//
// ZYViewController.m
// BlockTest
//
// Created by wanglixing on 14/11/2.
// Copyright © 2014年 zzz. All rights reserved.
// #import "ZYViewController.h" @interface ZYViewController () @property (nonatomic, retain) NSArray* colors; @end @implementation ZYViewController - (void)dealloc {
[_colors release]; [super dealloc];
} - (void)viewDidLoad {
self.view.backgroundColor = [UIColor whiteColor]; //数组包含了所有选项的标题。
UISegmentedControl* sc = [[UISegmentedControl alloc] initWithItems:@[@"红色", @"绿色", @"蓝色"]]; sc.frame = CGRectMake(, , , ); //使用 UIControlEventValueChanged
[sc addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:sc]; [sc release]; self.colors = @[[UIColor redColor], [UIColor greenColor], [UIColor blueColor]];
} - (void)valueChanged:(UISegmentedControl* )sender {
NSLog(@"%@", @(sender.selectedSegmentIndex)); //3.使用 delegate 对象调用协议方法进行传值。
if ([_delegate respondsToSelector:@selector(selectedColor:)]) {
//调用方法前需要判断 delegate 是否实现方法。
[_delegate selectedColor:_colors[sender.selectedSegmentIndex]];
}
} @end

//实现代理

//
// ViewController.m
// BlockTest
//
// Created by yejiong on 14/11/2.
// Copyright © 2014年 zzz. All rights reserved.
// #import "ViewController.h"
#import "ZYViewController.h" @interface ViewController () <ZYViewControllerDelegate> @end @implementation ViewController - (void)viewDidLoad {
self.view.backgroundColor = [UIColor whiteColor];
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { ZYViewController* vc = [[ZYViewController alloc] init]; .设置 delegate 属性。
vc.delegate = self; [self.navigationController pushViewController:vc animated:YES]; [vc release]; } #pragma mark - ZYViewControllerDelegate //4.实现协议方法,获取传过来的值。
- (void)selectedColor:(UIColor *)color {
self.view.backgroundColor = color;
} @end

使用 Block传值

//
// ZYBlockViewController.h
// BlockTest
//
// Created by yejiong on 14/11/2.
// Copyright © 2014年 zzz. All rights reserved.
// #import <UIKit/UIKit.h> //使用 block 传值分为 4 步。 //1.声明一个 block 类型,返回值表明传值的结果,参数表明传值的数据。
typedef void(^block)(UIColor* color); @interface ZYBlockViewController : UIViewController //2.声明一个 block 属性。
@property (nonatomic, copy) block block; @property (nonatomic, retain) UIColor* selectedColor; @end
//
// ZYBlockViewController.m
// BlockTest
//
// Created by yejiong on 14/11/2.
// Copyright © 2014年 zzz. All rights reserved.
// #import "ZYBlockViewController.h" @interface ZYBlockViewController () @property (nonatomic, retain) NSArray* colors; @end @implementation ZYBlockViewController - (void)dealloc {
[_colors release]; [_block release]; [_selectedColor release]; [super dealloc];
} - (void)viewDidLoad { self.view.backgroundColor = [UIColor whiteColor]; //数组包含了所有选项的标题。
UISegmentedControl* sc = [[UISegmentedControl alloc] initWithItems:@[@"红色", @"绿色", @"蓝色"]]; sc.frame = CGRectMake(, , , ); //使用 UIControlEventValueChanged
[sc addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:sc]; [sc release]; self.colors = @[[UIColor redColor], [UIColor greenColor], [UIColor blueColor]]; NSInteger index = [_colors indexOfObject:_selectedColor]; if (index != NSNotFound) {
sc.selectedSegmentIndex = index;
}
} - (void)valueChanged:(UISegmentedControl* )sender {
NSLog(@"%@", @(sender.selectedSegmentIndex)); //3.调用 block 进行传值。
if (_block) {
//调用 block 前需要判断 block 是否实现。
_block(_colors[sender.selectedSegmentIndex]);
}
} @end
//
// ViewController.m
// BlockTest
//
// Created by wanglixing on 14/11/2.
// Copyright © 2014年 zzz. All rights reserved.
// #import "ViewController.h"
#import "ZYBlockViewController.h" @interface ViewController () <ZYViewControllerDelegate> @end @implementation ViewController - (void)viewDidLoad {
self.view.backgroundColor = [UIColor whiteColor];
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { ZYBlockViewController* vc = [[ZYBlockViewController alloc] init]; //4.实现 block 并获取传过来的值。
vc.block = ^(UIColor* color){
//使用 color。
self.view.backgroundColor = color; //写在 push 前,push 后效果都一样,因为是同一个 UINavigationController
// [self.navigationController popViewControllerAnimated:YES];
}; vc.selectedColor = self.view.backgroundColor; [self.navigationController pushViewController:vc animated:YES]; [vc release];
} @end

代理和 block 传值的使用的更多相关文章

  1. 代理和block反向传值

    代理传值: // SendViewController.h #import <UIKit/UIKit.h> @protocol SendInFor <NSObject> -(v ...

  2. iOS中的代理和Block

    一.代理(Delegate) 1)含义 iOS中的代理,比如父母要去上班,到中午12点了,需要给宝宝喂饭吃,但是父母正在上班,这时需要有一个人来帮忙完成一些事情(需要有个保姆来帮忙给宝宝喂饭),此时, ...

  3. 使用代理和block写一个alertView

    代理: MyAlertView.h: @property (nonatomic,assign)id delegate; @protocol MyAlertViewDelegate <NSObje ...

  4. Swift基础--通知,代理和block的使用抉择以及Swift中的代理

    什么时候用通知,什么时候用代理,什么时候用block 通知 : 两者关系层次太深,八竿子打不着的那种最适合用通知.因为层级结构深了,用代理要一层一层往下传递,代码结构就复杂了 代理 : 父子关系,监听 ...

  5. 界面通信之block传值

    block传值有两种方式 ⽅式⼀: 使⽤block属性实现回调传值 ⽅式⼆: 在⽅法中定义block实现回调传值 方式一比较便于理解而且常用,下面介绍方式一是如何传值的 使用block属性传值和代理传 ...

  6. JDK动态代理和CGLIB的区别

    Aspect默认情况下不用实现接口,但对于目标对象,在默认情况下必须实现接口 如果没有实现接口必须引入CGLIB库 我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动 ...

  7. JDK动态代理和CGLib动态代理简单演示

    JDK1.3之后,Java提供了动态代理的技术,允许开发者在运行期间创建接口的代理实例. 一.首先我们进行JDK动态代理的演示. 现在我们有一个简单的业务接口Saying,如下: package te ...

  8. 属性传值,协议传值,block传值,单例传值四种界面传值方式

    一.属性传值 对于属性传值而言,相对于其它的三种 方法来说,是最基础,最简单的一种 方法,但,属性传值 有很大的局限性,因为是适用于第一个界面向第二个界面传 值,第二个向第三个界面传值等等.N界面向N ...

  9. iOS Block传值

    上个月,针对block恶补了一下,以为自己全部掌握了,其实不尽然. 昨天项目中在下载的时候用到,自己竟然不知道该从何下手,惭愧~ 情景是这个样子的:我写了个下载类,阴老师在调用时,将参数(sid,UR ...

随机推荐

  1. Hadoop-Map/Reduce实现实现倒排索引

    先来简单介绍一下什么是文档倒排索引 倒排索引是文档检索系统中最常见的数据结构,被广泛应用在全文搜索引擎上.主要用来存储某个单词(或词组)在一个文档或者一组文档中的存储位置的映射,即提供了一种根据内容来 ...

  2. ctags支持的语言

    http://ctags.sourceforge.net/languages.html Languages Supported by Exuberant Ctags: Ant Assembler AS ...

  3. 为Hadoop配置Win8.1授时服务器

    启动Windows服务,顺序如下: SSTPS(secure socket tunneling protocol service)服务 Telephony服务 Remote Access Connec ...

  4. maven 的各种命令

    mvn clean : 清理旧的文件 mvn clean compile :  清理 .编译 mvn clean test :       清理 .编译 .测试 mvn clean package : ...

  5. asp.net mvc 实体类成员变量标识示例

    检查不能为空 [Required] public string ID { get; set; } 检查最大长度 [StringLength(36, ErrorMessage = "长度不可超 ...

  6. 有趣的Node爬虫,数据导出成Excel

    最近一直没更新了诶,因为学习Backbone好头痛,别问我为什么不继续AngularJs~因为2.0要出来了啊,妈蛋!好,言归正传,最近帮我的好基友扒数据,他说要一些股票债券的数据.我一听,那不就是要 ...

  7. 转载Agile Development 敏捷软件开发介绍

    转载原地址: http://blog.csdn.net/wayne_ran/article/details/1601008 敏捷开发(agile development)是一种以人为核心.迭代.循序渐 ...

  8. 正整数的n次方求和

    引理: (Abel分部求和法) $$\sum_{k=1}^{n}a_{k}b_{k}=A_{n}b_{n}+\sum_{k=1}^{n-1}A_{k}(b_{k}-b_{k+1})$$其中$A_{k} ...

  9. Android改变了PDA市场格局

    看到一则消息<联想PDA助力306医院智慧医疗建设>,看完后感慨颇多:"大象"终于开始踩"蚂蚁"了!虽然主观上感觉这种做法很不地道,同时为传统PDA ...

  10. Intel® RealSense™ SDK Architecture

    In this article, we highlight some of the key changes that you can expect to see in the Intel RealSe ...