iOS 之通知
来源:互联网 发布:一对一私聊软件 编辑:程序博客网 时间:2024/05/18 03:42
//在接收通知.h文件里自定义一个属性button1,点击按钮触发事件
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface RootViewController : UIViewController
@property (nonatomic, retain)UIButton *button1;
@end
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
self.navigationItem.title = @"第一页";
self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
self.button1.frame = CGRectMake(240, 80, 100, 50);
self.button1.backgroundColor = [UIColor greenColor];
[self.button1 setTitle:@"第一页" forState:UIControlStateNormal];
[self.button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button1];
#warning第一步 注册通知 NSNotificationCenter 通知中心
//参数name:通知的名字,用于区别不同的消息通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"changeColor" object:nil];
}
#warning 第二步 实现通知方法
- (void)changeColor:(NSNotification *)notification
{
NSLog(@"第一个页面改变背景颜色");
#warning第五步 接手消息通知
//NSNotification类型转成object类型
//object类型强转成UIColor类型
UIColor *color = (UIColor *)[notification object];
self.view.backgroundColor = color;
}
- (void)button1Action:(UIButton *)button
{
FirstViewController *first = [[FirstViewController alloc]init];
[self.navigationController pushViewController:first animated:YES];
[first release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#warning 第三步 内存管理
- (void)dealloc
{
//name参数一定要跟注册通知时的name对应
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeColor" object:nil];
[super dealloc];
}
/*
#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
//在传递通知页面.m文件中
#import "secondViewController.h"
@interfacesecondViewController ()
@end
@implementation secondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorcyanColor];
self.navigationItem.title = @"通知";
self.button1 = [UIButtonbuttonWithType:UIButtonTypeCustom];
self.button1.frame = CGRectMake(240, 80, 100, 50);
self.button1.backgroundColor = [UIColor redColor];
[self.button1 setTitle:@"通知" forState:UIControlStateNormal];
[self.button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button1];
#pragma mark --- 延迟调用 延迟3秒后改变背景颜色
//参数1.延迟调用的方法
//参数2:延迟调用方法的参数
//参数3:延迟几秒调用 changeViewColor方法
[self performSelector:@selector(changeViewColor) withObject:nil afterDelay:3];
}
#warning 第四步 发送消息通知
- (void)button1Action:(UIButton *)button
{
//参数object是消息通知传的参数 可以是字符串字典等
[[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:[UIColor brownColor]];
[[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor1" object:[UIColor orangeColor]];
#pragma mark --- 中断延迟使用
NSLog(@"中断延迟");
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeViewColor) object:nil];
}
- (void)changeViewColor
{
self.view.backgroundColor = [UIColor greenColor];
}
- (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
- ios之通知
- IOS 之 通知NSNotificationCenter
- IOS之通知
- iOS 之通知
- iOS开发之 通知
- iOS开发之自定义通知
- 11.ios之通知机制
- iOS开发之通知机制
- ios开发系列之通知
- ios激情详解之通知
- IOS学习笔记之通知
- iOS学习基础教程之通知
- iOS开发之通知中心
- ios开发之本地通知
- ios SDK开发之键盘通知
- [转载]IOS开发之----远程推送通知
- ]IOS开发之----远程推送通知
- IOS开发之----远程推送通知
- 2015前端设计案例与趋势
- Linux下C编程-----文件操作(1) 通过系统调用简单操作标准输入、标准输出、标准错误
- 通过脚本来配置log4cplus
- java中线程安全的集合对象
- Log4j使用注意点
- iOS 之通知
- Out了,才来开博
- 矩阵三角分解,QR分解,奇异值分解
- 开源UI效果
- TFS 代码 API
- UNIX环境C语言编程(16)-伪终端
- 定时器 / 画圆
- Android中Handler引起的内存泄露
- Java继承并非是简单的代码复制