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

关键技术: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. 为jQuery添加Webkit的触摸方法支持

    前些日子收到邮件,之前兼职的一个项目被转给了其他人,跟进的人来问我相关代码的版权问题. 我就呵呵了. 这段代码是我在做13年一份兼职的时候无聊加上去的,为jQuery添加触摸事件的支持.因为做得有点无 ...

  2. python安装locustio报错error: invalid command 'bdist_wheel'的解决方法

    locust--scalable user load testing tool writen in Python(是用python写的.规模化.可扩展的测试性能的工具) 安装locustio需要的环境 ...

  3. C#设计模式系列:工厂方法模式(Factory Method)

    1. 工厂方法模式简介 1.1 定义 工厂方法模式定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法模式是以一个类的实例化延迟到其子类. Factory Method模式用于在不指定待创建 ...

  4. WPF绘制折线

    WPF后台绘制折线,填充到一个GRID下 private void btnPreview_Click(object sender, RoutedEventArgs e) { GridImg.Child ...

  5. MySQL的分页优化

    今天下午,帮同事重写了一个MySQL SQL语句,该SQL语句涉及两张表,其中一张表是字典表(需返回一个字段),另一张表是业务表(本身就有150个字段,需全部返回),当然,字段的个数是否合理在这里不予 ...

  6. Android开发之Shape详细解读

    日常开发中,我们会遇到一些Button.Textview...等控件的背景是圆角矩形.圆形...等,和android默认的控件背景矩形不一致,此时shape的作用就体现出来了,我们可以根据shape属 ...

  7. IO通道

    本文原创,转载需标明原处. 通道,主要负责传输数据,相当于流,但流只能是输入或输出类型中的其一,而通道则可以兼并二者. 通道的基类是:Channel boolean isOpen() void clo ...

  8. Java 8 Stream API详解--转

    原文地址:http://blog.csdn.net/chszs/article/details/47038607 Java 8 Stream API详解 一.Stream API介绍 Java8引入了 ...

  9. hibernate笔记--使用注解(annotation)方式配置单(双)向多对一的映射关系

    前面几篇都是介绍的用配置文件来实现实体类到数据库表的映射,这种方式是比较麻烦的,每一个pojo类都需要写一个相应的*.hbm.xml,无疑增加了很多代码量,不过也有优点就是利于维护,为了方便开发,Hi ...

  10. servlet中用注解的方式读取web.xml中的配置信息

    在学习servletContext的时候,我们知道了可以在web.xml中通过<context-param>标签来定义全局的属性(所有servlet都能读取的信息),并在servlet中通 ...