介绍:在iOS中类与类,控制器与控制器之间传值的方式有多种,分别是:单例传值、代理传值、通知传值、block传值或者一些偏好设置也可以用来传值。。。。
每一种传值方式都有各自的优点和缺点,针对不同情况选择符合需要的方式进行传值。这里我比较喜欢block方式传值,个人觉得简单好用,省却了不少代码。
下面简单给一个在控制器之间FirstViewController和SecondViewController使用block传值的例子:
在SecondViewController中:
SecondViewController.h文件
//
// SecondViewController.h
// block传值
//
// Created by mac on 16/6/16.
// Copyright © 2016年 JDYang. All rights reserved.
//
#import <UIKit/UIKit.h>
/**
* 定义一个block别名,方便使用,该block返回值为void类型,有一个参数为字符串类型
*/
typedef void (^ReturnValueByBlock)(NSString *text);
@interface SecondViewController : UIViewController
/**
* 声明一个block变量
*/
@property (copy,nonatomic)ReturnValueByBlock returnBlock;
/**
* 声明一个实例方法,block代码块作为参数
*/
-(void)returnValue:(ReturnValueByBlock) block;
@end
#import <UIKit/UIKit.h>
/**
* 定义一个block别名,方便使用,该block返回值为void类型,有一个参数为字符串类型
*/
typedef void (^ReturnValueByBlock)(NSString *text);
@interface SecondViewController : UIViewController
/**
* 声明一个block变量
*/
@property (copy,nonatomic)ReturnValueByBlock returnBlock;
/**
* 声明一个实例方法,block代码块作为参数
*/
-(void)returnValue:(ReturnValueByBlock) block;
@end
SecondViewController.m文件
//
// SecondViewController.m
// block传值
//
// Created by mac on 16/6/16.
// Copyright © 2016年 JDYang. All rights reserved.
//
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
//返回
self.view.backgroundColor = [UIColor greenColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(back:)];
}
//返回
-(void)back:(UIBarButtonItem *)sender{
[self.navigationController popViewControllerAnimated:YES];
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
//返回
self.view.backgroundColor = [UIColor greenColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(back:)];
}
//返回
-(void)back:(UIBarButtonItem *)sender{
[self.navigationController popViewControllerAnimated:YES];
//给block参数赋值
self.returnBlock(@"我是赛亚人!");
}
//通过实例方法回传值
-(void)returnValue:(ReturnValueByBlock)block{
self.returnBlock = block;
}
@end
//通过实例方法回传值
-(void)returnValue:(ReturnValueByBlock)block{
self.returnBlock = block;
}
@end
在FirstViewController中:
FirstViewController.m文件
//
// ViewController.m
// block传值
//
// Created by mac on 16/6/16.
// Copyright © 2016年 JDYang. All rights reserved.
//
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建按钮
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
btn.center = self.view.center;
btn.backgroundColor = [UIColor redColor];
[btn setTitle:@"入栈" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//入栈
-(void)push:(UIButton *)sender{
SecondViewController *secondVC = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secondVC animated:YES];
//接收回传的值
[secondVC returnValue:^(NSString *text) {
NSLog(@"%@",text);
}];
}
@end