目标:在两个独立的控制器的界面之间进行反向传值
关键技术: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了
*/