iOS 之通知

//在接收通知.h文件里自定义一个属性button1,点击按钮触发事件

#import <UIKit/UIKit.h>

#import "FirstViewController.h"

@interface RootViewController : UIViewController

@property (nonatomic, retain)UIButton *button1;

@end

//在接收通知.m文件里

#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"

@interface secondViewController ()

@end

@implementation secondViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor cyanColor];

    self.navigationItem.title = @"通知";

    self.button1 = [UIButton buttonWithType: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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值