代理和 block 传值的使用
//
// 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 传值的使用的更多相关文章
- 代理和block反向传值
代理传值: // SendViewController.h #import <UIKit/UIKit.h> @protocol SendInFor <NSObject> -(v ...
- iOS中的代理和Block
一.代理(Delegate) 1)含义 iOS中的代理,比如父母要去上班,到中午12点了,需要给宝宝喂饭吃,但是父母正在上班,这时需要有一个人来帮忙完成一些事情(需要有个保姆来帮忙给宝宝喂饭),此时, ...
- 使用代理和block写一个alertView
代理: MyAlertView.h: @property (nonatomic,assign)id delegate; @protocol MyAlertViewDelegate <NSObje ...
- Swift基础--通知,代理和block的使用抉择以及Swift中的代理
什么时候用通知,什么时候用代理,什么时候用block 通知 : 两者关系层次太深,八竿子打不着的那种最适合用通知.因为层级结构深了,用代理要一层一层往下传递,代码结构就复杂了 代理 : 父子关系,监听 ...
- 界面通信之block传值
block传值有两种方式 ⽅式⼀: 使⽤block属性实现回调传值 ⽅式⼆: 在⽅法中定义block实现回调传值 方式一比较便于理解而且常用,下面介绍方式一是如何传值的 使用block属性传值和代理传 ...
- JDK动态代理和CGLIB的区别
Aspect默认情况下不用实现接口,但对于目标对象,在默认情况下必须实现接口 如果没有实现接口必须引入CGLIB库 我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动 ...
- JDK动态代理和CGLib动态代理简单演示
JDK1.3之后,Java提供了动态代理的技术,允许开发者在运行期间创建接口的代理实例. 一.首先我们进行JDK动态代理的演示. 现在我们有一个简单的业务接口Saying,如下: package te ...
- 属性传值,协议传值,block传值,单例传值四种界面传值方式
一.属性传值 对于属性传值而言,相对于其它的三种 方法来说,是最基础,最简单的一种 方法,但,属性传值 有很大的局限性,因为是适用于第一个界面向第二个界面传 值,第二个向第三个界面传值等等.N界面向N ...
- iOS Block传值
上个月,针对block恶补了一下,以为自己全部掌握了,其实不尽然. 昨天项目中在下载的时候用到,自己竟然不知道该从何下手,惭愧~ 情景是这个样子的:我写了个下载类,阴老师在调用时,将参数(sid,UR ...
随机推荐
- Fast Intro To Java Programming (2)
Java局部变量 局部变量声明在方法.构造方法或者语句块中: 局部变量在方法.构造方法.或者语句块被执行的时候创建,当它们执行完成后,变量将会被销毁: 访问修饰符不能用于局部变量: 局部变量只在声明它 ...
- 第二百七十七天 how can I 坚持
开玩笑要有个度,哎,或许这就是缘分,很容易受别人影响吗? 中国人为什么会经常抱怨,不抱怨,挺好. 睡觉,红颜祸水,老婆是要能一起 生活的,不是失去,是上天在帮我,哈哈.
- UVa 1640 The Counting Problem (数学,区间计数)
题意:给定两个数m, n,求从 m 到 n 中0-9数字各出现了多少次. 析:看起来挺简单的,其实并不好做,因为有容易想乱了.主要思路应该是这样的,分区间计数,先从个位进行计,一步一步的计算过来.都从 ...
- 浅析网站开发中的 meta 标签的作用
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- Listview上下滚动崩溃
利用CursorAdapter在ListView中显示Cursor中不同同类型的item,加载均正常,滚动时报如下错误: 11-28 15:18:16.703: E/InputEventReceive ...
- HttpMessageConverter用法
HttpMessageConverter接口定义 * Strategy interface that specifies a converter that can convert from and t ...
- android Studio gradle so的加载
最近在使用Android Studio开发android项目,我的项目是由gradle构建的,gradle的优势文档上有说明,当你不断使用中,我越来越发现,太TMD方便啦,优势大大的明显. 打包,功能 ...
- Usage of readonly and const
Many new learners can not make sure the usage scenarios of readonly and const keywords. In my opinio ...
- 通过注册表查看已安装 .NetFramework 版本
gci "hklm:\SOFTWARE\Microsoft\NET Framework Setup\NDP" | % {(Get-ItemProperty registry::$_ ...
- C++ 预编译头文件
1.解决什么问题? C++ 编译器是单独,分别编译的,每个cpp文件,进行预编译(也就是对#include,define 等进行文本替换),生成编译单元.编译单元是一个自包含文件,C++编译器对编译单 ...