目标:在两个独立的控制器的界面之间进行反向传值

关键技术: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进行界面之间的反向传值的更多相关文章

  1. 03-UIKit、VC之间正向反向传值、代理

    目录: 一.正向传值 二.反向传值 三.代理模式 回到顶部 正向传值:就是把第一个界面的值传给第二个界面显示,其简单实现方法 1 首先在第一个界面中要有一个textField输入框,一个按钮butto ...

  2. Apple Watch开发之界面之间的正向传值

    主要分两种,一种是故事板中通过segue进行的传值,第二种是使用代码. 贴代码 24行代码是代码进行传值 35是故事板中的

  3. iOS Block界面反向传值

    在上篇博客 <iOS Block简介> 中,侧重解析了 iOS Block的概念等,本文将侧重于它们在开发中的应用. Block是iOS4.0+ 和Mac OS X 10.6+ 引进的对C ...

  4. iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)

    iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)   使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值: 先说一下思想: ...

  5. IOS Block 反向传值

    1.在需要像上一个界面传值的.h 文件实现代理方法 @property (nonatomic, copy) void(^isOpenHandler)(BOOL) ; 2.在执行操作的时候需要江操作的结 ...

  6. iOS 再谈 代理传值,block反向传值

    本贴的例子是:有A和B两个界面,要实现的效果就是先让A跳转到B,然后B中有个颜色的参数,当B跳转到A时,把这个颜色的参数传递给A,在A中利用这个颜色改变自己界面的颜色. 第1步:在发送者(界面B)中, ...

  7. 代理和block反向传值

    代理传值: // SendViewController.h #import <UIKit/UIKit.h> @protocol SendInFor <NSObject> -(v ...

  8. block 反向传值回调

    /** *  block 反向传值回调 */ //在第二个控制器中 //   (1)声明block,在基类中已写好 //   (2)写好传值方法 //(1) typedef void (^Return ...

  9. IOS- 最简单的反向传值- block

    block 常用于反向传值 声明 返回值类型 (^block)(参数列表) 调用 闭包的名字=^(参数列表){}: 闭包的名字(): 如: void(^aaaaa)(int num,NSString ...

随机推荐

  1. Python yield与实现

    Python yield与实现  yield的功能类似于return,但是不同之处在于它返回的是生成器. 生成器 生成器是通过一个或多个yield表达式构成的函数,每一个生成器都是一个迭代器(但是迭 ...

  2. Dynamics CRM导出数据到Excel

    原创地址:http://www.cnblogs.com/jfzhu/p/4276212.html 转载请注明出处 Pivot Table是微软BI的一个重要工具,所以这里讲一下Dynamics CRM ...

  3. Module Zero之Nuget包

    返回<Module Zero学习目录> ABP module-zero已经发布在了nuget上了.这里是所有的包列表. Abp.Zero module zero的核心包. Abp.Zero ...

  4. Eclipse 调试技巧

    条件断点 顾名思义,是指当发生某种情况或者触发某种条件的情况下命中断点.常用的情形就是for循环中某个变量为xx值的时候命中断点类似的. 做法1:在debug视图中,BreakPoint View将所 ...

  5. 基于redis实现可靠的分布式锁

    什么是锁 今天要谈的是如何在分布式环境下实现一个全局锁,在开始之前先说说非分布式下的锁: 单机 – 单进程程序使用互斥锁mutex,解决多个线程之间的同步问题 单机 – 多进程程序使用信号量sem,解 ...

  6. Data Flow的Error Output

    一,在Data Flow Task中,对于Error Row的处理通过Error Output Tab配置的. 1,操作失败的类型:Error(Conversion) 和 Truncation. 2, ...

  7. Sql Server系列:子查询

    1 子查询概念 子查询是嵌套在另一个查询中的普通T-SQL查询.在有一个SELECT语句通过使用小括号创建子查询,作为另一个查询的部分数据或条件的基础. 子查询通常用于满足以下某个需求: ◊ 将一个查 ...

  8. Android NDK开发Hello Word!

    在之前的博客中已经为大家介绍了,如何在win环境下配置DNK程序,本篇我将带大家实现一个简单的Hello jni程序,让大家真正感受一下NDK开发的魅力.这里我们选择使用C+JAVA开发Android ...

  9. 平衡二叉树AVL删除

    平衡二叉树的插入过程:http://www.cnblogs.com/hujunzheng/p/4665451.html 对于二叉平衡树的删除采用的是二叉排序树删除的思路: 假设被删结点是*p,其双亲是 ...

  10. Android okHttp网络请求之Get/Post请求

    前言: 之前项目中一直使用的Xutils开源框架,从xutils 2.1.5版本使用到最近的xutils 3.0,使用起来也是蛮方便的,只不过最近想着完善一下app中使用的开源框架,由于Xutils里 ...