代理和 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 ...
随机推荐
- <Chapter 2>2-2-2-1.介绍JSPs,JSTL,和EL(Introducing JSPs, JSTL, and EL)
现在,我们的时钟显示了UTC时区的时间.我们希望我们的应用可以让用户自定义时区,并且为将来的访问记住用户的偏好.为了做到这一点,我们使用Google帐户来识别哪个用户正在使用这个应用. 在我们深入了解 ...
- Using Apache Maven
Apache Maven是一个软件项目管理的综合工具(management and comprehension tool).可以将WAR文件部署到App Engine中.为了加快部署的速度,App E ...
- ubuntu使用问题与解决记录[持续更新]
1. 添加到计划任务 为脚本增加可执行权限 sudo chmod +x yeelink.sh 将脚本加入cronjob(计划任务) sudo crontab -e 在cornjob文件中添加下面一行, ...
- 虚拟桌面基础架构(VDI)与终端服务和传统PC对比
VDI(Virtual Desktop Infrastructure),即虚拟桌面基础架构,正迅速成为一个热门词汇,它将颠覆企业向终端用户交付应用的游戏规则.这篇专题就是想通过VDI与两种传统技术的对 ...
- oracle 字符集转换:AL32UTF8->ZHS16GBK
select userenv('language') from dual; --修改Oracle数据库字符集为ZHS16GBK : SQL>conn / as sysdba; SQL>sh ...
- JSF 2 textbox example
In JSF, you can use the <h:inputText /> tag to render a HTML input of type="text", t ...
- CFileDialog的用法
CFileDialog 在MSDN中的函数原形 CFileDialog::CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, L ...
- Oracle-11g 从表空间删除数据文件
从表空间删除数据文件前提条件 如果欲从表空间中删除数据文件,那么该数据文件必须为空,否则将报出"ORA-03262: the file is non-empty"的错误. 从表 ...
- HibernateDaoSupport的使用
1.HibernateDaoSupport是有spring提供的一个hibernate模版工具类,或不多说,直接上代码 接口 IGenericDao.java package org.hibernat ...
- Rstudio设置永久工作路径
Rstudio中 getwd() 获取工作路径 setwd() 设置工作路径 但这种方式设置后每次打开都要重新设置,现在介绍一种设置永久路径的方法