有两个界面A—>B,现将B界面的值传到A界面,应在B界面进行block的声明以及实现
B界面代码如下:
#import <UIKit/UIKit.h>
typedef void(^SendNumBlock)(NSString *num);
@interface ZJNextViewController : UIViewController
-(void)sendNum:(SendNumBlock)block;
@end
#import "ZJNextViewController.h"
@interface ZJNextViewController ()
@end
@implementation ZJNextViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 200, 50, 50);
[button setBackgroundColor:[UIColor orangeColor]];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)sendNum:(SendNumBlock)block
{
block(@"yan");
}
-(void)back
{
[self dismissViewControllerAnimated:YES completion:nil];
}
A界面在需要调用的时候 调用B界面的block方法即可
#import "ViewController.h"
#import "ZJNextViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 200, 50, 50);
[button setBackgroundColor:[UIColor orangeColor]];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitle:@"跳转" forState:UIControlStateNormal];
[button addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeSystem];
nextBtn.frame = CGRectMake(200, 300, 50, 50);
[nextBtn setBackgroundColor:[UIColor orangeColor]];
[nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[nextBtn setTitle:@"传值" forState:UIControlStateNormal];
[nextBtn addTarget:self action:@selector(showText) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:nextBtn];
}
-(void)showText
{
ZJNextViewController *nextVc = [[ZJNextViewController alloc]init];
[nextVc sendNum:^(NSString *num) {
NSLog(@"viewController:%@",num);
}];
}
-(void)push
{
ZJNextViewController *nextVc = [[ZJNextViewController alloc]init];
[self presentViewController:nextVc animated:YES completion:nil];
}