1、在您需要传值出去的控制器头文件里声明:
@property (nonatomic,copy) void(^stringblockr)(int age);
self.stringblockr(15);
SecondViewController *send = [[SecondViewController alloc]init];
send.stringblockr = ^(int age) {
NSLog(@"%d",age);
};
以下是整个流程:
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (nonatomic,copy) void(^stringblockr)(int age);
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor purpleColor];
UIButton *ba = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100,50)];
ba.backgroundColor = [UIColor blueColor];
[ba addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:ba];
}
- (void)popAction {
self.stringblockr(15);
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor redColor];
}
- (IBAction)btnClick:(id)sender {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 100, 100, 30)];
[self.view addSubview:label];
label.backgroundColor = [UIColor orangeColor];
SecondViewController *send = [[SecondViewController alloc]init];
send.stringblockr = ^(int age) {
NSLog(@"%d",age);
};
[self.navigationController pushViewController:send animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}