代理和 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 ...
随机推荐
- Sqlserver作业-手把手带你体验
所谓Sql Server作业就是按照规定的时间执行指定的脚本,如果在SQL Server 里需要定时或者每隔一段时间执行某个存储过程或3200字符以内的SQL语句时,可以用管理-SQL Server代 ...
- windwos iis 7.5 使用html 报405错误
今天遇到了这个问题,网上搜一下基本上都是下面的答案: <form> 没有指定action的话就是文件自身了. .html本身是不可执行的,如果要修改的话,在IIS中站点属性- 主目录 - ...
- android - python 自动化测试 移动互联网 - SegmentFault
android - python 自动化测试 移动互联网 - SegmentFault splinter
- Hadoop应用开发实战案例 第1周
本课程的基础课程是,Hadoop数据分析平台课程.相信,能看我本博文的朋友,是有一定的基础了. 只是前个课程是讲解,这个课程是应用. 第一层是:数据源层,代表有生产线上的数据,比如关系型数据库orca ...
- Codeforces 599D Spongebob and Squares(数学)
D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calculati ...
- Linux 的进程组、会话、守护进程
一.进程组ID 每个进程都属于一个进程组.每个进程组有一个领头进程.进程组是一个或多个进程的集合,通常它们与一组作业相关联,可以接受来自同一终端的各种信号.每个进程组都有唯一的进程组ID(整数,也可以 ...
- Winfrom强大的自动更新程序
推荐一:.Net 小型软件自动更新库(SimpAutoUpdater) http://www.fishlee.net/soft/simple_autoupdater/usage.html 下载地址:h ...
- SQL性能优化工具TKPROF
全名为Trace Kernel Profile,用来格式化跟踪文件,是一个命令行工具. 主要的格式例如以下: tkprof tracefile outputfile - tracefile:要分 ...
- Swift学习笔记一
最近计划把Swift语言系统学习一下,然后将MagViewer用这种新语言重构一次,并且优化一下,这里记录一下Swift的学习笔记. Swift和Objective-C相比,在语法和书写形式上做了很多 ...
- 第29题:推断一个序列是否是还有一个push序列的pop序列
github:https://github.com/frank-cq/MyTest 第29题:输入两个整数序列,当中一个序列表示栈的push顺序,推断还有一个序列有没有可能是相应的pop顺序.为了简单 ...