block 反向传值详细解释

  1. 两个控制器,viewcontroller---push---secondviewcontroller。。。viewcontroler中有一个button(用来push到第二个控制器,还有一个lable(用来展示第二个页面blcok传过来的值
  2. 下面贴过来两个控制器中详细代码
viewcontroller.h
typedef NSString *(^mssblock)(NSString *);
@interface ViewController : UIViewController
@property (nonatomic ,copy) mssblock mss;
@property (weak, nonatomic) IBOutlet UILabel *showtexlable;
@end

viewcontroller.m

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)pushpressed:(id)sender {
    SecondViewController *vc =[SecondViewController new];
    [self.navigationController pushViewController:vc animated:YES];
    [vc returnmessage:^(NSString *str) {
        self.showtexlable.text =str;
    }];
}
@end
secondviewcontroller.h
#import 

typedef void(^mssblcokdemo)(NSString *str);
@interface SecondViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *textfild;
@property (nonatomic ,strong) mssblcokdemo block;
-(void)returnmessage:(mssblcokdemo)block;
@end
secondviewcontroller.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (IBAction)backpressed:(id)sender {
    self.block(_textfild.text);
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)returnmessage:(mssblcokdemo)block{
    self.block=block;
}
@end

(由于系统键盘问题,键盘转换大小写卡顿很严重,mini盒子就这样,不适合开发,自己穷不怪公司,你们得谅解呀,不懂得地方可以留言,或者加qq 517769958)

你可能感兴趣的:(block 反向传值详细解释)