在第二個頁面的.h中
#import <UIKit/UIKit.h>
//塊語法回傳第一步,聲明
typedef void(^ShowTextBlock)(NSString *text);
@interface NextViewController : UIViewController
//快語法回傳第二步,聲明塊類型的對象
@property(nonatomic,copy) ShowTextBlock showTextBlock;
@end
在第二個頁面的.m中
#import "NextViewController.h"
@interface NextViewController ()
@end
@implementation NextViewController
- (IBAction)clickReturn:(UITextField *)sender {
//塊語法回傳第三步,適當(dāng)?shù)奈恢谜{(diào)用
_showTextBlock(sender.text);
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
在第一個頁面的.h中獲取:
#import "MyViewController.h"
#import "NextViewController.h"
@interface MyViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation MyViewController
- (IBAction)click:(id)sender {
NextViewController *nextVC = [NextViewController new];
//第四步:獲取到回調(diào)信息.
nextVC.showTextBlock = ^(NSString *text){
_label.text = text;
};
[self presentViewController:nextVC animated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}