接着昨天继续,今天使用通知实现两个控制器之间的传值, 这个过程比较简单。
要求:bViewController中textField用户输入完毕后跳转到aViewController同时要更改它的label标签显示
实现:
第一步:在bViewController消失时发送一个通知,向通知中心发送
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] postNotificationName:kChangeLabelTextNotification object:_textField.text];
self.block(_textField.text);
}
#define kChangeLabelTextNotification @"changeLabelTextNotification"
- (instancetype)init
{
if (self = [super init]) {
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(notificationAction:)
name:kChangeLabelTextNotification
object:nil];
}
return self;
}
- (void)notificationAction:(NSNotification *)notification
{
_label.text = notification.object;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:kChangeLabelTextNotification object:nil];
}