//
// 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

最新文章

  1. ajax用get刷新页面元素在IE下无效解决~~
  2. eclipse运行项目发生Unsupported major.minor version 52.0错误
  3. 瞎BB
  4. android widget包说明与应用
  5. HTTP常见错误代码总结
  6. javascript值和引用
  7. JavaScript提高:003:easy UI实现tab页面自适应问题
  8. 《学习Opencv》第五章 习题6
  9. linux 编译安装apache
  10. Java 与C++的各种优势与弱点--学习更新中
  11. window7如何配置修改环境变量
  12. Axure RP8 注册码
  13. 2、Storm中的一些概念理解
  14. OpenGL and Vulkan resources
  15. netty为啥要二次开发
  16. xcode10 - 打ipa上蒲公英或者fire.im
  17. NOIP2017普及组T2题解
  18. Check which .NET Framework version is installed
  19. UNIX环境编程学习笔记(20)——进程管理之exec 函数族
  20. C语言对文件的基本操作

热门文章

  1. HTML编码
  2. Hadoop学习之--Capaycity Scheduler配置参数说明
  3. 用java获取歌曲文件的专辑封面元信息
  4. HTML5每日一练之input新增加的5种其他类型1种标签应用
  5. 现在,UICollectionViews有了简单的重排功能
  6. App Extension编程指南(iOS8/OS X v10.10)中文版
  7. linux下安装apache详解
  8. android知乎小圆圈刷新效果
  9. Python 结巴分词模块
  10. JQuery Plugin 1 - Simple Plugin