ViewController.m##
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *myTextField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//注册消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheme:) name:@"change" object:nil];
_myTextField.delegate = self;
//键盘弹出调节界面高度
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(adjustHeight:) name:UIKeyboardWillShowNotification object:nil];
//键盘回收返回原界面高度
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(backHeight:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)adjustHeight:(NSNotification *)sender{
[UIView beginAnimations:@"键盘弹出" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
CGRect newRect = self.view.frame;
newRect.origin.y -= 250;
self.view.frame = newRect;
[UIView commitAnimations];
}
-(void)backHeight:(NSNotification *)sender{
[UIView beginAnimations:@"键盘落下" context:nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
CGRect newRect = self.view.frame;
newRect.origin.y += 250;
self.view.frame = newRect;
[UIView commitAnimations];
}
//接收消息后做出的反应
-(void)changeTheme:(NSNotification *)sender{
self.view.backgroundColor = sender.userInfo[@"color"] ;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
SecondViewController.m##
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
//注册消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheme:) name:@"change" object:nil];
}
-(void)changeTheme:(NSNotification *)sender{
self.view.backgroundColor = sender.userInfo[@"color"] ;
}
- (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
ThirdViewController.m##
#import "ThirdViewController.h"
@interface ThirdViewController ()
@end
@implementation ThirdViewController
- (void)viewDidLoad {
[super viewDidLoad];
//注册消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheme:) name:@"change" object:nil];
}
////发送消息(向所有name是@"change"的页面发送消息)
//[[NSNotificationCenter defaultCenter]postNotificationName:@"change" object:nil userInfo:@{@"color":[UIColor lightGrayColor]}];//userInfo:发送消息时传递的信息(字典);
-(void)changeTheme:(NSNotification *)sender{
NSNotification *notification = sender;
self.view.backgroundColor = notification.userInfo[@"color"] ;
}
- (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
SettingViewController.m##
#import "SettingViewController.h"
@interface SettingViewController ()
@end
@implementation SettingViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)changeCloor:(UIButton *)sender {
NSLog(@"修改所有页面背景颜色");
//发送消息(向所有name是@"change"的页面发送消息)
[[NSNotificationCenter defaultCenter]postNotificationName:@"change" object:nil userInfo:@{@"color":[UIColor lightGrayColor]}];//userInfo:发送消息时传递的信息(字典);
//object 这个参数有点像二次确认的意思,就是在同一个通知name的情况下还可以通过object再次进行细分通知。就拿上面这个小demo说,如果object为空,接收方会接受所有名字为giveName的通知。但是如果object不为空,接收方就会只接收名字为giveName的而且object正确的通知。
}
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheme:) name:@"change" object:nil];
/*
通知 原理一问度娘一大堆解释,我在这就用我自己的理解给大家狂谈一下,刚刚上面也提及到了,基本通知有三步走:post发送通知,NSNotificationCenter通知中心处理通知,addobserve接收通知。不论发送通知还是接收通知都只有一条路可走,那就是通知中心。如果你还能找到另外一个路走,那你真是酷炫牛逼呲啦冒火花了。通知中心是一个单例类,管理系统的所有通知事件,不论是我们手动发出的还是系统的通知。在创建通知的时候有两种情况:
1、只是传递动作,不传递具体的信息内容详情
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject;
2、传递具体的信息内容,用字典传递。
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end