使用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 ...
随机推荐
- 使用GDB调试Go语言
用Go语言已经有一段时间了,总结一下如何用GDB来调试它! ps:网上有很多文章都有描述,但是都不是很全面,这里将那些方法汇总一下 GDB简介 GDB是GNU开源组织发布的⼀一个强⼤大的UNIX下的 ...
- 如何在 ASP.NET MVC 中集成 AngularJS(2)
在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...
- bootstrap-modal 学习笔记 源码分析
Bootstrap是Twitter推出的一个开源的用于前端开发的工具包,怎么用直接官网 http://twitter.github.io/bootstrap/ 我博客的定位就是把这些年看过的源码给慢慢 ...
- 深入理解脚本化CSS系列第六篇——脚本化伪元素的6种方法
× 目录 [1]动态样式 [2]CSS类[3]setAttribute()[4]CSSRule对象添加[5]空样式覆盖[6]CSSRule对象删除 前面的话 我们可以通过计算样式来读取伪元素的样式信息 ...
- android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果
需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果, 总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...
- MyEclipse的项目中把 java EE 5 Libraries 删掉后怎么重新导入
myeclipse中鼠标右击项目->properties->java Build Path=>Libraries=>Add Library...=>选择MyEclipse ...
- javascript之一切皆为对象2
其实呢,“函数function”和“对象object”之间还有这么一句话:对象是通过函数来创建的,而函数却又是一种对象. 这个函数是一种对象,上节中“Javascript之一切皆为对象1”也清楚的阐述 ...
- 应用程序框架实战十三:DDD分层架构之我见
前面介绍了应用程序框架的一个重要组成部分——公共操作类,并提供了一个数据类型转换公共操作类作为示例进行演示.下面准备介绍应用程序框架的另一个重要组成部分,即体系架构支持.你不一定要使用DDD这样的架构 ...
- 通过监控线程状态来保证socket服务器的稳定运行
云平台中使用的socket服务器是我们自己定义一套通信协议,并通过C#实现的一个socket服务. 该服务目前是和web服务一起运行在IIS容器中,通过启动一个永不退出的新线程来监听端口. 在开发的初 ...
- 使用Windows Service Wrapper快速创建一个Windows Service
前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...