iOS--Notification
2014-04-04 10:46
405 查看
Notification(消息中心)是iOS观察者模式的另一种实现,相比较KVO跟容易操作,Notification也可用于跨界面传值(传值小懒会再开一个模块),也是很常用的传值写法,废话不多说,上代码!
先创建RootViewController(根视图)、SecondViewController(第二个视图)。
RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSNotificationCenter *notice = [NSNotificationCenter defaultCenter];//启用消息中心
[notice addObserver:self selector:@selector(action:) name:@"color" object:nil];//添加观察者
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
[self.view addSubview:button];
[button release];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:self action:@selector(toSecond) forControlEvents:UIControlEventTouchUpInside];
}
-(void)toSecond
{
SecondViewController *second = [[SecondViewController alloc]init];
[self presentViewController:second animated:YES completion:NULL];
[second release];
}
-(void)action:(NSNotification *)sender
{
[self.view setBackgroundColor:[UIColor orangeColor]];
NSLog(@"%@",sender);
}
SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view setBackgroundColor:[UIColor blueColor]];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
[self.view addSubview:button];
[button release];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:self action:@selector(toRoot) forControlEvents:UIControlEventTouchUpInside];
}
-(void)toRoot
{
NSNotificationCenter *notice = [NSNotificationCenter defaultCenter];//启用消息中心
// [notice postNotificationName:@"color" object:nil];
[notice postNotificationName:@"color" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"123",@"456", nil]];//发送请求及消息
[self dismissViewControllerAnimated:YES completion:NULL];
}
小懒习惯用mrc,所以在最后别忘了加上dealloc函数
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"color" object:nil];
[super dealloc];
}
很简单的小demo,当我们完成两次点击button后的界面切换时,会有字符串打印出来。
首先当我们要用到notification时要做个声明:
NSNotificationCenter *notice = [NSNotificationCenter defaultCenter];//启用消息中心
然后添加观察者,以name字段为key响应事件,一个对象可以添加多个观察者,可以有多个响应事件。
[notice addObserver:self selector:@selector(action:) name:@"color" object:nil];
最后在另外的界面也要先启用消息中心,然后发送请求即可:
[notice postNotificationName:@"color" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"123",@"456", nil]];
以“color”为关键字寻找对应观察者,userInfo为发送对象(即传值)。让我们回头在看看这个函数:
-(void)action:(NSNotification *)sender
{
[self.view setBackgroundColor:[UIColor orangeColor]];
NSLog(@"%@",sender);
}
这是观察者的响应函数,sender参数边包含了传递过来的值。
ok,已经讲完了,是不是特别简单!
先创建RootViewController(根视图)、SecondViewController(第二个视图)。
RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSNotificationCenter *notice = [NSNotificationCenter defaultCenter];//启用消息中心
[notice addObserver:self selector:@selector(action:) name:@"color" object:nil];//添加观察者
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
[self.view addSubview:button];
[button release];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:self action:@selector(toSecond) forControlEvents:UIControlEventTouchUpInside];
}
-(void)toSecond
{
SecondViewController *second = [[SecondViewController alloc]init];
[self presentViewController:second animated:YES completion:NULL];
[second release];
}
-(void)action:(NSNotification *)sender
{
[self.view setBackgroundColor:[UIColor orangeColor]];
NSLog(@"%@",sender);
}
SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view setBackgroundColor:[UIColor blueColor]];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
[self.view addSubview:button];
[button release];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:self action:@selector(toRoot) forControlEvents:UIControlEventTouchUpInside];
}
-(void)toRoot
{
NSNotificationCenter *notice = [NSNotificationCenter defaultCenter];//启用消息中心
// [notice postNotificationName:@"color" object:nil];
[notice postNotificationName:@"color" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"123",@"456", nil]];//发送请求及消息
[self dismissViewControllerAnimated:YES completion:NULL];
}
小懒习惯用mrc,所以在最后别忘了加上dealloc函数
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"color" object:nil];
[super dealloc];
}
很简单的小demo,当我们完成两次点击button后的界面切换时,会有字符串打印出来。
首先当我们要用到notification时要做个声明:
NSNotificationCenter *notice = [NSNotificationCenter defaultCenter];//启用消息中心
然后添加观察者,以name字段为key响应事件,一个对象可以添加多个观察者,可以有多个响应事件。
[notice addObserver:self selector:@selector(action:) name:@"color" object:nil];
最后在另外的界面也要先启用消息中心,然后发送请求即可:
[notice postNotificationName:@"color" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"123",@"456", nil]];
以“color”为关键字寻找对应观察者,userInfo为发送对象(即传值)。让我们回头在看看这个函数:
-(void)action:(NSNotification *)sender
{
[self.view setBackgroundColor:[UIColor orangeColor]];
NSLog(@"%@",sender);
}
这是观察者的响应函数,sender参数边包含了传递过来的值。
ok,已经讲完了,是不是特别简单!
相关文章推荐
- iOS 如何选择delegate、notification、KVO?
- iOS的一些常见系统Notification(系统消息)
- 【iOS开发】推送跳转处理&&UIApplicationLaunchOptionsRemoteNotificationKey
- registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later
- iOS-OC-提交审核:Missing Push Notification Entitlement
- iOS 本地推送 UILocalNotification
- ios本地通知UILocalNotification以及区分谁触发了通知
- ios push notification
- iOS 监听 出发 Home键 NSNotificationCenter UIApplicationWillResignActiveNotification
- iOS - APNs(Apple Push Notification Services)推送通知
- iOS APNS 訊息推播 – Apple Push Notification Service 介紹
- iOS 10 本地推送UNUserNotificationCenter
- When to use Delegation, Notification, or Observation in iOS
- iOS iOS8中 问题"registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later" 解决方案
- iOS 本地推送 UILocalNotification
- iOS开发-进阶:NSNotificationCenter 使用姿势详解
- iOS推送之本地推送(iOS Notification Of Local Notification)
- ios的notification机制是同步的
- IOS 如何选择delegate、notification、KVO?
- ios notification