secondViewController
secondViewController.h
@interface secondViewController : UIViewController
// 需要传过去的字符串
@property (nonatomic, strong)NSString *string;
@property (nonatomic, strong)NSMutableDictionary* dictionary;
// 点击按钮跳转到第一界面
@property (nonatomic, strong)UIButton* button;
@end
secondViewController.m
#import "secondViewController.h"
@interface secondViewController ()
@end
@implementation secondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_string = [NSString stringWithFormat:@"Secon界面传过来的字符串"];
_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_button setTitle:@"到界面一" forState:UIControlStateNormal];
[_button addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
_button.frame = CGRectMake(100, 200, 100, 40);
[self.view addSubview:_button];
_dictionary = [[NSMutableDictionary alloc] init];
[_dictionary setObject:_string forKey:@"key1"];
self.view.backgroundColor = [UIColor orangeColor];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"inform" object:nil userInfo:_dictionary];
postNotificationName
:之后的参数就是这个通知的名字,要和要和接收者中的名字一样,才能让接收者正确接收。object
:接收对象userInfo
: 携带的参数,为字典类型的数据,在例子中我携带了一个字典,因为有时候我们要传递的参数不只是一个,所以把东西全部放在通知里面,在接收者中,根据字典里面的键来取出里面的值。_dictionary
是我之前定义的一个字典实例。- (void)pressButton {
// 发送通知并回传数据
// 注册通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"inform" object:nil userInfo:_dictionary];
[self dismissViewControllerAnimated:YES completion:nil];
}
ViewController
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong)UIButton * button;
@property (nonatomic, strong)UILabel* label;
@end
ViewController.m
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiReceived:) name:@"TransDataNoti" object:nil];
#import "ViewController.h"
#import "secondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 250, 40)];
[self.view addSubview:_label];
_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_button setTitle:@"到界面2" forState:UIControlStateNormal];
_button.frame = CGRectMake(100, 300, 100, 40);
[_button addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
// 注册观察者,用于接受界面二的通知,接收通知的名称必须和发送通知的名称保持一致才能收到
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNoticed:) name:@"inform" object:nil];
}
- (void)pressButton {
secondViewController* secondView = [[secondViewController alloc] init];
[self presentViewController:secondView animated:YES completion:nil];
}
// 观察者接受函数,
// 这里接受传值并赋值给当前页面的label
- (void)receiveNoticed:(NSNotification*)sender {
_label.text = sender.userInfo[@"key1"];
}
// 移除通知
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
通知传值 Block传值 协议传值这三个传值都倾向于逆向传值的应用
后续还会学习并更新内容