博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NSNotification与NSNotificationCenter
阅读量:4596 次
发布时间:2019-06-09

本文共 2271 字,大约阅读时间需要 7 分钟。

//通知 NSNotification
//NSNotification是一个model,与日常项目中的model是一样的,比如你的Movie,Card.代表一个通知.包含name(NSString),object(id),userinfo(NSDictionary),提供了创建方法.以及查看通知信息的方法.
//NSNotification是信息.需要通过通知中心发布.
//NSNotificationCenter主要负责通知的处理
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    //通知 NSNotification
    //NSNotification是一个model,与日常项目中的model是一样的,比如你的Movie,Card.代表一个通知.包含name(NSString),object(id),userinfo(NSDictionary),提供了创建方法.以及查看通知信息的方法.
    //NSNotification是信息.需要通过通知中心发布.
    //NSNotificationCenter主要负责通知的处理
    
    
    self.view.backgroundColor = [UIColor redColor];
    //标准的写法.应该创建一个view的子类,在loadView方法中,吧self.view设置为view的子类对象,此处我们简写,吧button的创建写在ViewDidLoad之中.
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setFrame:CGRectMake(100, 100, 100, 100)];
    [btn setTitle:@"bufangjia" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(postNotification) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    NSNotificationCenter *notificationcenter = [NSNotificationCenter defaultCenter];
    [notificationcenter addObserver:self selector:@selector(aa:) name:@"不放假" object:@"张三"];
    [notificationcenter addObserver:self selector:@selector(cc:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)cc:(NSNotification *)a
{
    NSLog(@"%@",a.name);
    NSLog(@"进入后台");
}
- (void)aa:(NSNotification *)a
{
    NSLog(@"%@",a.name);
}
- (void)postNotification
{
    NSLog(@"button点击事件");
    NSNotificationCenter *notificationcenter = [NSNotificationCenter defaultCenter];
    [notificationcenter postNotificationName:@"不放假" object:@"张三"];
    //Notification 同步,看btn的执行顺序
    NSLog(@"点击结束");
    
}
//当控制器被释放掉的时候,一定要将通知remove掉,不然会引起程序crash
- (void)dealloc
{
    NSNotificationCenter *notificationcenter = [NSNotificationCenter defaultCenter];
    [notificationcenter removeObserver:self name:nil object:nil];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

转载于:https://www.cnblogs.com/xukunhenwuliao/p/3576228.html

你可能感兴趣的文章
软件开发人员必须具备的20款免费的windows下的工具(转载)
查看>>
MyBatis源码探索
查看>>
python 迭代
查看>>
File查看目录
查看>>
去除ActionBar的方法
查看>>
STM8S——Universal asynchronous receiver transmitter (UART)
查看>>
Flink - state管理
查看>>
Apache Kafka - KIP-42: Add Producer and Consumer Interceptors
查看>>
ArcGIS JS Demo
查看>>
webservice发布问题,部署iis后调用不成功
查看>>
Koch 分形,海岸线,雪花
查看>>
ubuntu系统下Python虚拟环境的安装和使用
查看>>
IOS7开发~新UI学起(二)
查看>>
软件过程度量和CMMI模型概述
查看>>
数据结构(DataStructure)与算法(Algorithm)、STL应用
查看>>
Linux与Windows xp操作系统启动过程
查看>>
linux运维、架构之路-Kubernetes1.13离线集群部署双向认证
查看>>
[Leetcode]Substring with Concatenation of All Words
查看>>
Gem install rmagick 报错问题~
查看>>
验证一个方法触发时机
查看>>