通知传值

来源:互联网 发布:递延所得税资产 知乎 编辑:程序博客网 时间:2024/06/03 20:32

-> 大多反向传值(必须先注册观察者,再发送通知,才能收到通知的原因)

////  ViewController.m//  -//#import "ViewController.h"#import "SecondVC.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    [_nextButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];    // 1.注册观察者    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(observerDidReceived:) name:@"PassToPreviousNotification" object:nil];}// 2.观察到,执行方法- (void)observerDidReceived:(NSNotification*)notification{    _receiveLabel.text = [notification.userInfo objectForKey:@"valueString"];}-(void)nextPage{    SecondVC * secondVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"SecondVC"];    [self presentViewController:secondVC animated:true completion:nil];}// 3.释放通知- (void)dealloc{    [[NSNotificationCenter defaultCenter] removeObserver:self];}@end
////  SecondVC.m////#import "SecondVC.h"@interface SecondVC ()@end@implementation SecondVC- (void)viewDidLoad {    [super viewDidLoad];    [_backButton addTarget:self action:@selector(backToPreviousPage) forControlEvents:UIControlEventTouchUpInside];}- (void)backToPreviousPage{    // 发送通知    [[NSNotificationCenter defaultCenter] postNotificationName:@"PassToPreviousNotification" object:self userInfo:@{@"valueString":_contentTextF.text}];    [self dismissViewControllerAnimated:true completion:nil];}@end
原创粉丝点击