iOS 之通知

来源:互联网 发布:一对一私聊软件 编辑:程序博客网 时间:2024/05/18 03:42

//在接收通知.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"

@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



0 0