使用block进行界面之间的反向传值
目标:在两个独立的控制器的界面之间进行反向传值
关键技术:block
代码编写及运行环境:Xcode6.4 / 模拟器8.4
语言:Objective-C
注:使用纯代码实现,不使用xib/storyboard
效果图:



前期注意:

代码实现如下:
1.

//
// AppDelegate.h
// blockPassValue
//
// Created by xiaoC on 16/9/28.
// Copyright (c) 2016年 xiaoLeC. All rights reserved.
// #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
//
// AppDelegate.m
// blockPassValue
//
// Created by xiaoC on 16/9/28.
// Copyright (c) 2016年 xiaoLeC. All rights reserved.
// #import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible]; self.window.rootViewController = [[ViewController alloc] init]; return YES;
} - (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
} - (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
} - (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
} - (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
} - (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
} @end
2.

//
// ViewController.h
// blockPassValue
//
// Created by xiaoC on 16/9/28.
// Copyright (c) 2016年 xiaoLeC. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
//
// ViewController.m
// blockPassValue
//
// Created by xiaoC on 16/9/28.
// Copyright (c) 2016年 xiaoLeC. All rights reserved.
// #import "ViewController.h"
#import "YFSecondViewController.h" @interface ViewController ()
@property(nonatomic,strong)UILabel *lab;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor grayColor]; //添加一个跳转的按钮
UIButton *jumpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
jumpBtn.frame = CGRectMake(, , , );
jumpBtn.backgroundColor = [UIColor yellowColor];
[jumpBtn setTitle:@"跳转" forState:UIControlStateNormal];
[jumpBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[jumpBtn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:jumpBtn]; //创建一个label,接收传回来的值
_lab = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
_lab.backgroundColor = [UIColor yellowColor];
_lab.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_lab];
} //调用该方法跳转到YFSecondViewController控制器界面
-(void)jump
{
YFSecondViewController *yfSecondV = [[YFSecondViewController alloc] init];
[self presentViewController:yfSecondV animated:YES completion:nil]; //block代码块
yfSecondV.block = ^void (NSString *str){ //给label设置返回来的值
_lab.text = str;
};
} @end
3.

//
// YFSecondViewController.h
//
//
// Created by xiaoLeC on 16/9/28.
//
// #import <UIKit/UIKit.h> typedef void(^myBlock)(NSString *str); @interface YFSecondViewController : UIViewController @property (nonatomic,copy) myBlock block; @end
//
// YFSecondViewController.m
//
//
// Created by xiaoLeC on 16/9/28.
//
// #import "YFSecondViewController.h" @interface YFSecondViewController () @end @implementation YFSecondViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor]; //来一些数据
NSArray *arr = @[@"",@"",@"我是90后"];
for (int i=; i<; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(, (i*)+, , );
[btn setTitle:arr[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
} } -(void)back:(UIButton *)sender
{
//调用block,要在销毁控制器之前调用block
self.block(sender.currentTitle); //销毁控制器
[self dismissViewControllerAnimated:YES completion:nil];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
/*
block的回调的使用步骤
1.声明 : 在谁那里调用就在谁那里声明
实现代码
typedef void(^MyBlock)(NSString *name);//block的重命名
@property (nonatomic,copy) MyBlock block;//block的声明
2.实现 : 谁要装值就在谁那里实现
实现代码
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self presentViewController:secondVC animated:YES completion:nil];//在这里没用导航控制器,用presentViewController来进入下一个视图
//block实现
secondVC.block = ^void(NSString *name)
{
_label.text = name;
};//block的位置摆放很作用的,因为它是一个函数,不过一定不能放在使用它的对象的外面和前面就好了
3.调用 : 谁要传值就在谁那里调用
self.block(@"呵呵");//block的调用
总结一句话:block用在不同视图控制器之间的值回传,回传还有代理、单例,在回传中最简单的就是用block了
*/
使用block进行界面之间的反向传值的更多相关文章
- 03-UIKit、VC之间正向反向传值、代理
目录: 一.正向传值 二.反向传值 三.代理模式 回到顶部 正向传值:就是把第一个界面的值传给第二个界面显示,其简单实现方法 1 首先在第一个界面中要有一个textField输入框,一个按钮butto ...
- Apple Watch开发之界面之间的正向传值
主要分两种,一种是故事板中通过segue进行的传值,第二种是使用代码. 贴代码 24行代码是代码进行传值 35是故事板中的
- iOS Block界面反向传值
在上篇博客 <iOS Block简介> 中,侧重解析了 iOS Block的概念等,本文将侧重于它们在开发中的应用. Block是iOS4.0+ 和Mac OS X 10.6+ 引进的对C ...
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值) 使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值: 先说一下思想: ...
- IOS Block 反向传值
1.在需要像上一个界面传值的.h 文件实现代理方法 @property (nonatomic, copy) void(^isOpenHandler)(BOOL) ; 2.在执行操作的时候需要江操作的结 ...
- iOS 再谈 代理传值,block反向传值
本贴的例子是:有A和B两个界面,要实现的效果就是先让A跳转到B,然后B中有个颜色的参数,当B跳转到A时,把这个颜色的参数传递给A,在A中利用这个颜色改变自己界面的颜色. 第1步:在发送者(界面B)中, ...
- 代理和block反向传值
代理传值: // SendViewController.h #import <UIKit/UIKit.h> @protocol SendInFor <NSObject> -(v ...
- block 反向传值回调
/** * block 反向传值回调 */ //在第二个控制器中 // (1)声明block,在基类中已写好 // (2)写好传值方法 //(1) typedef void (^Return ...
- IOS- 最简单的反向传值- block
block 常用于反向传值 声明 返回值类型 (^block)(参数列表) 调用 闭包的名字=^(参数列表){}: 闭包的名字(): 如: void(^aaaaa)(int num,NSString ...
随机推荐
- C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword)
C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword) +BIT祝威+悄悄在此留下版了个权的信息说: C#申请一 ...
- 顶级的JavaScript框架、库、工具及其使用
几乎每隔一个星期,就有一个新的 JavaScript 库席卷网络社区!Web 社区日益活跃.多样,并在多个领域快速成长.想要研究每一个重要的 JavaScript 框架和库,是个不可能完成的任务.接下 ...
- 台式电脑、笔记本快捷选择Boot(启动项快捷键)大全
我们在安装系统时,会去设置电脑是从硬盘启动.U盘启动.光驱启动.网卡启动. 一般设置的方法有两种:一种是进BIOS主板菜单设置启动项顺序:另一种就是我在这里要介绍的快捷选择启动项. 以下是网友整理的各 ...
- 【原】Python 用例:打印一个 Header Box
sentence= input("Input Sentence: ") screen_width=80 text_width= len(sentence) box_width= t ...
- jsp 分页, 判断是第一页,和最后一页.
<% //页的行数 int pagesize =20; //当前页 int currentPage = 1; try { currentPage = Integer.parseInt(reque ...
- Sql Server系列:数据库操作
1 创建数据库 1.1 CREATE DATABASE语法 CREATE DATABASE database_name [ ON [ PRIMARY ] <filespec> [ ,... ...
- OpenCASCADE Curve Length Calculation
OpenCASCADE Curve Length Calculation eryar@163.com Abstract. The natural parametric equations of a c ...
- Memory Management in Open Cascade
Open Cascade中的内存管理 Memory Management in Open Cascade eryar@163.com 一.C++中的内存管理 Memory Management in ...
- 深入学习jQuery元素过滤
× 目录 [1]索引过滤 [2]内容过滤 前面的话 过滤是jQuery扩展的一个重要的内容.jQuery选择器中的一个重要部分就是过滤选择器.除了过滤选择器,还有专门的元素过滤的方法.本文将详细介绍j ...
- javascript四种类型识别的方法
× 目录 [1]typeof [2]instanceof [3]constructor[4]toString 前面的话 javascript有复杂的类型系统,类型识别则是基本的功能.javascrip ...