文件1:ViewController.m
#import "ViewController.h"
#import "Animal.h"
#import "MyViewController.h"
@interface ViewController ()<MyViewControllerDelegate>
//在扩展中定义三个实例变量
{
NSString *_name;
int _age;
Animal *_animal;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置值
_name = @"A";
_age = 10;
_animal = [[Animal alloc] init];
//利用通知中心添加一次观察,如果同一个对象对同一个通知添加了多次观察,就会多次接收通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didNotif:) name:@"name" object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
//观察到通知后执行的方法
- (void) didNotif:(NSNotification *)notification
{
NSLog(@"%@", notification.name);
NSLog(@"%@", notification.object);
NSLog(@"%@", notification.userInfo);
NSDictionary *dic = notification.userInfo;
_name = dic[@"name"];
NSNumber *num = (NSNumber *)dic[@"age"];
_age = num.intValue;
NSLog(@"%@: %d", _name, _age);
//收到此通知后,移除对本通知的观察
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"name" object:nil];
}
//向通知中心注销
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//在storyboard跳转前的准备动作
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//通过segue得到目标对象
if ([segue.identifier isEqualToString:@"FirstToSecond"]) {
MyViewController *myCtrl = segue.destinationViewController;
myCtrl.delegate = self;//通过协议实现两个文件之间的数据传输
myCtrl.vcl = self;//通过属性实现两个文件之间的数据传输
myCtrl.name = _name;//通过属性实现两个文件之间的数据传输
myCtrl.block = ^{//通过block实现两个文件之间的数据传输
return _name;
};
}
}
- (NSString *)nameForViewController:(MyViewController *)viewController
{
return _name;
}
- (void)print
{
NSLog(@"%@: %d: %@", _name, _age, _animal.name);
}
@end
文件2:MyViewController.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
@class MyViewController;
@protocol MyViewControllerDelegate <NSObject>
- (NSString *)nameForViewController:(MyViewController *)viewController;
@end
@interface MyViewController :UIViewController
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *(^block)(void);
@property (nonatomic,weak) id<MyViewControllerDelegate>delegate;
@property (nonatomic,weak) ViewController *vcl;
@end
文件3:MyViewController.m
#import "MyViewController.h"
@interface MyViewController ()
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
if (_delegate&&[_delegate respondsToSelector:@selector(nameForViewController:)]) {
_name = [_delegate nameForViewController:self];
}
NSLog(@"%@",_name);
if (_block) {
_block();
}
NSLog(@"+++ZXC%@",_name);
//发布通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"name" object:nil userInfo:@{@"name":@"Wangwu", @"age":@345}];
// [];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end