使用代理Delegate实现反向传值
1)定义协议.方法
2)声明一个属性,记录代理对象
3)在合适的时机,给代理方发消息
BViewController.h
#import <UIKit/UIKit.h>
@class BViewController;
/** 定义协议 */
@protocol BViewControllerDelegate <NSObject>
/** 定义方法 */
-(void)BViewController:(BViewController *)bViewController didFinishInputMsg:(NSString *)msg;
@end
@interface BViewController : UIViewController
/** 声明一个属性,记录代理对象 */
@property (nonatomic,weak) id<BViewControllerDelegate> delegate;
@end
BViewController.m
@interface BViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
@implementation BViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)clickDelegate:(id)sender {
/** 界面dismiss前给代理方发消息 */
[self.delegate BViewController:self didFinishInputMsg:self.textField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
AViewController.m
1)遵守协议
2)实现方法
3)设置代理
AViewController.m
#import "AViewController.h"
#import "BViewController.h"
/** 遵守BViewController协议 */
@interface AViewController ()<BViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation AViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
/** 获取storyboard segue 设置代理 */
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
BViewController *bViewContrller = segue.destinationViewController;
bViewContrller.delegate = self;
}
#pragma mark - BViewControllerDelegate
/** 实现BViewControllerDelegate的方法 */
-(void)BViewController:(BViewController *)bViewController didFinishInputMsg:(NSString *)msg {
self.label.text = msg;
}
@end
使用通知NSNotification实现反向传值
BViewController.h
1)声明全局通知名称
2)发送全局通知
#import <UIKit/UIKit.h>
/** 声明一个全局通知名称 */
#define kNotificationDidFinishInputMsg @"kNotificationDidFinishInputMsg"
BViewController.m
@interface BViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
@implementation BViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)clickNotification:(id)sender {
/** 发送全局通知 */
[[NSNotificationCenter defaultCenter]postNotificationName:kNotificationDidFinishInputMsg object:@{@"msg" : self.textField.text}];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
AViewController.m
1)收听通知
2)注销通知
#import "AViewController.h"
#import "BViewController.h"
@interface AViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation AViewController
- (void)viewDidLoad {
[super viewDidLoad];
/** 添加收听者 */
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getNotification:) name:kNotificationDidFinishInputMsg object:nil];
}
/** 当收到通知,实现该方法 */
-(void)getNotification:(NSNotification *)notification {
self.label.text = notification.object[@"msg"];
}
/** dealloc方法里需注销通知 */
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
使用block实现反向传值
BViewController.h
1)声明block
2)传参
#import <UIKit/UIKit.h>
@interface BViewController : UIViewController
/** 声明block */
@property (nonatomic,copy) void (^finishInputMsgBlock)(NSString *msg);
@end
BViewController.m
#import "BViewController.h"
@interface BViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
@implementation BViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)clickBlock:(id)sender {
/** 传入参数 */
self.finishInputMsgBlock(self.textField.text);
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
AViewController.m
1)推出界面时回调block
#import "AViewController.h"
#import "BViewController.h"
@interface AViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation AViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
/** 获取storyboard segue 回调block */
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
BViewController *bViewContrller = segue.destinationViewController;
bViewContrller.finishInputMsgBlock = ^(NSString *msg) {
self.label.text = msg;
};
}
@end