Block传值代码如下:
#import "TRRootBViewController.h"
#import "TRRootAViewController.h"
@interface TRRootBViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property(nonatomic,strong)TRRootAViewController *Avc; //跳转的页面
@end
@implementation TRRootBViewController
- (IBAction)btn:(id)sender {
self.Avc=[[TRRootAViewController alloc]initWithNibName:@"TRRootAViewController" bundle:nil];
__weak typeof(self) weakself=self;
self.Avc.backValue=^(NSString *stingValue){
weakself.label.text=stingValue;
};
[self presentViewController:self.Avc animated:YES completion:nil];
}
#import <UIKit/UIKit.h>
@interface TRRootAViewController : UIViewController
// block属性
@property(nonatomic,copy)void(^backValue)(NSString *stringValue);
@end
#import "TRRootAViewController.h"
@interface TRRootAViewController ()
@end
@implementation TRRootAViewController
- (IBAction)back:(UIButton *)sender {
[self dismissViewControllerAnimated:YES completion:^{
self.backValue(@"hello word");
}];
}
点击返回后效果如下: