标签:
#import <UIKit/UIKit.h> @interface BViewController : UIViewController @property(nonatomic,strong)void (^ returnStr) (NSString *str); - (void)returnStr:(void (^) (NSString *str))str; @end
1.首先要更正自己的一个观点,属性为block函数是能够被strong修饰的(自己不知道怎么搞的,一直以为其职能被assign修饰)
2.那就是关于block的传值了,直接代码
在A控制器中
- (IBAction)tiaoZhuan:(id)sender { //A控制器中调用B控制器中带有block函数的方法 [self.BVC returnStr:^(NSString *str) { self.label.text = str; [self.view setNeedsDisplay]; }]; [self presentViewController:self.BVC animated:YES completion:nil]; }
在B控制器中 .h文件
#import <UIKit/UIKit.h> @interface BViewController : UIViewController //这里的block属性需要用strong修饰 @property(nonatomic,strong)void (^ returnStr) (NSString *str); - (void)returnStr:(void (^) (NSString *str))str; @end
.m文件
- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"B-viewDidLoad"); UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; [button setTitle:@"返回" forState:UIControlStateNormal]; [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)returnStr:(void (^) (NSString *str))str { self.returnStr = str; //str(@"我是反回来的"); } //返回函数 - (void)back:(UIButton *)sender { self.returnStr(@"我是反回来的"); [self dismissViewControllerAnimated:YES completion:nil]; }
标签:
原文地址:http://www.cnblogs.com/feiyudexingfu/p/4612472.html