博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS菜鸟学习——NSNotification
阅读量:4120 次
发布时间:2019-05-25

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

    NSNotificationNSNotificationCenter是离不开的。NSNotificationCenter就像是一个广播站,它会播放各种广播(即NSNotification),你可以选择打开(即添加observer)某一个或者多个广播来收听,而广播不是在播放的,每当广播站送(post NSNotification)一次你就收到一次,你可以在收到广播之后做出相应的响应(即NSNotification在observer中 的 selector)。当然,广播的听众也可以不只是一个。

     比如说,UIApplicationWillEnterForegroundNotification。每当app入background,将会送一个名字叫做UIApplicationWillEnterForegroundNotificationNSNotification,如果你添加为个NSNotification的observer,那么就会接收到个NSNotification,你可以在接收到之后做出响应,比如说保存未保存的数据等。

     下面来实现一个简单的NSNotification。

     首先需要定义一种NSNotification。

     我们把定义NSNotification的类叫做FileDownloadManager(假定它内部实现了下载功能)。

     首先,要给NSNotification起一个名字。在FileDownloadManager.h中定义

extern NSString *const FILE_DIDDOWNLOAD_NOTIFICATION;
      
FileDownloadManager.m中给它赋值。

NSString *const FILE_DIDDOWNLOAD_NOTIFICATION    = @"FILE_DIDDOWNLOAD_NOTIFICATION";

     FILE_DIDDOWNLOAD_NOTIFICATION就是这个NSNotification的名字。

       每当FileDownloadManager完成一次下载将发总一次NSNotification,即定义及发送NSNotification。

NSNotification *notification = [NSNotification notificationWithName: FILE_DIDDOWNLOAD_NOTIFICATION   object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:fileName, @"fileName", nil]];[[NSNotificationCenter defaultCenter] postNotification:notification];

   这样发送了一个NSNotification,其中userInfo是一个NSDictionary,其中包含了需要送的信息,里就包含了下的文件名。

      然后就是接收,比如我要在一个叫做ViewController的类中就收。那么需要加入以下代

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileDidDownload:)  name: FILE_DIDDOWNLOAD_NOTIFICATION  object:nil];

   这样,每当FILE_DIDDOWNLOAD_NOTIFICATION发送时,ViewController就会接收到。并用ViewController中的fileDidDownload:方法。

      fileDidDownload方法可以这么写

- (void)fileDidDownload:(NSNotification *)notification{   NSString *fileName = [notification.userInfo valueForKey:@"fileName"];   ……}

      这样就取出了传过来的值

      每当添加observer我们就要在适当的时候remove这个observer。比如说你在viewWillAppear中添加的observer那么每次viewWillAppear执行都会添加一次observer,那么每当发送一个FILE_DIDDOWNLOAD_NOTIFICATION这个ViewController就会用多次fileDidDownload:。remove observer方法如下。

[[NSNotificationCenter defaultCenter] removeObserver:self name: FILE_DIDDOWNLOAD_NOTIFICATION object:nil];

  NSNotification与线程。

     这是需要注意的一点,还是拿上面的例子来说。比如我在一个background线程中postNotification,那么如果你不指定线程,fileDidDownload:方法中的代码也将在postNotification同一个线程中执行。所以比如我们要在fileDidDownload中执行一些view的更新等操作,就必须保证在主线程中执行。可以通过dispatch queue等方法来实现。

转载地址:http://bmvpi.baihongyu.com/

你可能感兴趣的文章
Linux查看mac地址
查看>>
Linux修改ip
查看>>
MySQL字段类型的选择与MySQL的查询效率
查看>>
Java的Properties配置文件用法【续】
查看>>
JAVA操作properties文件的代码实例
查看>>
IPS开发手记【一】
查看>>
Java通用字符处理类
查看>>
文件上传时生成“日期+随机数”式文件名前缀的Java代码
查看>>
Java代码检查工具Checkstyle常见输出结果
查看>>
北京十大情人分手圣地
查看>>
Android自动关机代码
查看>>
Android中启动其他Activity并返回结果
查看>>
2009年33所高校被暂停或被限制招生
查看>>
GlassFish 部署及应用入门
查看>>
iWatch报错: Authorization request cancled
查看>>
iWatch报错: Authorizationsession time out
查看>>
如何运行从网上下载的iWatch项目详细步骤.
查看>>
X-code7 beta error: warning: Is a directory
查看>>
Error: An App ID with identifier "*****" is not avaliable. Please enter a different string.
查看>>
X-code beta 开发iWatch项目,运行没有错误,但是某些操作一点就崩,而且找不错误的原因场景一
查看>>