例子:将Vc1 控制器接收传值 ,Vc2控制器发送传值消息,为了便于区别和代理传值的 使用 , 将两者放到 一起其中Vc1控制器里 两个UITextField一个接收代理传值的使用,一个接收block 的使用 。
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<</span>secondViewControllerDelegate>
- (IBAction)gengxin;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UITextField *secTextField;
@property (nonatomic,strong) SecondViewController *secVc;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
SecondViewController *secondVc = [[SecondViewController alloc] init];
[secondVc returnText:^(NSString *showText) {
NSLog(@"---------%@------------",showText);
self.secTextField.text = showText;
}];
self.secVc = secondVc;
self.secVc.delegate = self;
}
- (IBAction)gengxin {
[self presentViewController:self.secVc animated:YES completion:nil];
}
#pragma mark - secondViewControllerDelegate
- (void)setTextfield:(NSString *)text{
self.textField.text = text;
NSLog(@"%@",text);
}
@end
#import "ViewController.h"
@protocol secondViewControllerDelegate
@optional - (void)setTextfield:(NSString *)text;
@end
typedef void(^returnBlock)(NSString *showText);
@interface SecondViewController : ViewController
@property (nonatomic,strong) id delegate;
//声明block 属性
@property (nonatomic,strong)returnBlock returnTextBlock;
//声明 调用方法
- (void) returnText: (returnBlock)block;
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@property (nonatomic,weak) UITextField *textField;
@property (nonatomic,weak) UIButton *saveButton;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
UITextField *textField =[[UITextField alloc] init];
[self.view addSubview:textField];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
self.textField = textField;
self.textField.frame = CGRectMake(100, 100, 100, 100);
UIButton *save = [[UIButton alloc] init];
[self.view addSubview:save];
self.saveButton = save;
self.saveButton.frame = CGRectMake(200, 200, 100, 70);
[self.saveButton setTitle:@"确定" forState:UIControlStateNormal];
self.saveButton.titleLabel.textColor = [UIColor blackColor];
[self.saveButton addTarget:self action:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];
}
- (void) buttonClick:(id)sender{
if ([self.delegate respondsToSelector:@selector(setTextField:)]) {
NSString *text =self.textField.text;
[self.delegate setTextfield:text];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) returnText:(returnBlock)block
{
self.returnTextBlock = block;
}
- (void)viewDidDisappear:(BOOL)animated
{
//即将消失的时候
if (self.returnTextBlock !=nil) {
self.returnTextBlock(self.textField.text);
}
NSLog(@"======%@=======",self.returnTextBlock);
}
@end
两者之间 的理解方式相同 ,在Vc1传值的里面,将returenText:方法 里的 block值传入 vc2控制器里,在控制器Vc2跳转的时候 ,可以判断属性里有值 ,然后再重新加载vc1的时候 就可以将值写入控制器里的secTextField里了。