这里是不同的对象之间的通知, 不是本地通知.

一开始玩, 很挠头, 后来发现原来只是对象init的过程出了问题.

首先, 新建一个简单的单controller的工程.

然后打开它的ViewController.m文件

@interface ViewController ()
@property  NotifyObserver *obj;    //这里是关键, 应该有一个property是另一个要通知的类的, 我之前写在了viewDidLoad里面, 结果死活通知没有响应, 其实原因是这个对象在viewDidLoad方法完成后就自动销毁了, 还通知个屁.

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   self.obj=[[NotifyObserver alloc] init]; //这里init了一个要通知的对象.

//NotifyObserver *obj=[[NotifyObserver alloc] init]; 之前是这么写的, 折腾了两个小时, 应该抽自己....
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)notifyButtonPressed:(id)sender {
    NSLog(@"pressed");
    //这里开始通知了, 还留下一个问题是如何拿到userInfo, 先不管, 总之发送了一个名为updateMessage的通知, 内容就是my object.
    NSNotification *notification = [[NSNotification alloc] initWithName:@"updateMessage" object:@"my object" userInfo:@{@"Status": @"Success"}];

[[NSNotificationCenter defaultCenter] postNotification:notification];

}

@end

谁要接收这个通知, 就要去注册一下

@implementation NotifyObserver

-(id)init{
    NSLog(@"init");

//在init方法里面注册自己是一个观察者, 然后定义自己只接收"updateMessage"的通知, 别乱七八糟的啥都通知我, 另外, 如果通知到了, 麻烦运行update方法, 记得后面有个":"意思是自己是带形参的方法, Java母语的人笑而不语
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(update:) name:@"updateMessage" object:nil];   
    return self;
}
-(void)dealloc{
   //记得这个对象要被销毁时, 要移除注册
   [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//这个update方法就是通知的回调, 一旦通知来了, 就麻烦运行这个方法.
-(void) update :(NSNotification *)notification
{
    NSLog(@"update");
    NSString* str = (NSString*)[notification object];//这里取出刚刚从过来的字符串
    NSLog(@"%@",str);
}

@end

几乎一天时间学会通知Notification, 够慢的....

越来越觉得iOS就是简单啊.....

通知, 委托, 这两个设计模式iOS玩得溜啊...

iOS的通知Notification的更多相关文章

  1. iOS消息通知Notification的用法

    1.发送消息 NSNotification *notification = [NSNotification notificationWithName:@"selectPosition&quo ...

  2. ios 消息通知

    苹果的通知分为本地通知和远程通知,这里主要说的是远程通知 历史介绍 iOS 3 - 引入推送通知UIApplication 的 registerForRemoteNotificationTypes 与 ...

  3. 通知 Notification 详解

    效果 通知栏-刚收到通知时 通知栏-收到通知几秒后 标准视图 大视图-下滑前是标准视图 大视图-下滑后显示大视图 自定义通知 讲解 Notification,俗称通知,是一种具有全局效果的通知,它展示 ...

  4. IOS 本地通知 UILocalNotification

    IOS 本地通知 UILocalNotification [本文章第四部分中的代码逻辑来自网上的借鉴,并非我自己原创] 大概一个月前,我开始跟着做IOS项目了.学习C++,了解Objective-C, ...

  5. 适配 通知 Notification 通知渠道 前台服务 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  6. IOS中通知中心(NSNotificationCenter)

    摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广.   IOS中通知中心NSNotificationCenter应用总结 一.了 ...

  7. iOS 本地通知 操作

    iOS 本地通知 操作 1:配置通知:然后退出程序: UILocalNotification *localNotif = [[UILocalNotification alloc] init]; loc ...

  8. IOS NSNotification 通知

    一. 先看下官方对NSNotification通知的解释 1. NSNotification 通知 @interface NSNotification : NSObject <NSCopying ...

  9. 浏览器桌面通知Notification探究

    首先说明,这篇博文不是科普讲解的,而是立flag研究的,是关于浏览器消息自动推送,就是下面这个玩意: 最近常常在浏览器看到这样的消息推送,还有QQ.com的推送,现在我对这个不了解,不知道叫消息自动推 ...

随机推荐

  1. java中 set,list,array(集合与数组)相互转换

      public static Object[] List2Array(List<Object> oList) { Object[] oArray = oList.toArray(new ...

  2. C# WebSocket 服务端示例代码 + HTML5客户端示例代码

    WebSocket服务端 C#示例代码 using System; using System.Collections.Generic; using System.Linq; using System. ...

  3. Linux 基本收集

    ifconfig eth0 192.168.1.223 切换到root账号开始是$符号输入su输入root密码转换成# 就变成了root账号 dr 查看盘符ls /etc/ 查看etc文件夹下面的文件 ...

  4. PLSQL 的简单命令之二

    --1. 查询工资大于12000的员工姓名和工资 --2. 查询员工号为176的员工的姓名和部门号 ' --3. 选择工资不在5000到12000的员工的姓名和工资 --4. 选择雇用时间在1998- ...

  5. jose4j / JWT Examples

    jose4j / JWT Examples View History JSON Web Token (JWT) Code Examples Producing and consuming a sign ...

  6. pip报ssl错误解决

     InsecurePlatformWarning: A true SSLContext object is not available.   # yum -y install openssl-deve ...

  7. Swift游戏实战-跑酷熊猫 13 二段跳的实现

    这节内容我们来实现熊猫的二段跳. 要点: 二段跳的逻辑: 逻辑一,第一次点击屏幕,status就会变成jump. 逻辑二,第二次点击屏幕,status就会变成jump2. 逻辑三,当status变成j ...

  8. 数据库SQL 查询

    查询 1.简单查询 select * from info(表名)   --查所有数据 select  code(列名),name(列名)  from 表名        --查指定列的数据 selec ...

  9. 今天遇到的一个问题(windows的ssh客户端连接不到虚拟机Ubuntu)

    今天比较郁闷,想用windows上的ssh客户端连接虚拟机中的Ubuntu. 但是死活连不上,之前是能脸上的,所以比较郁闷. 我首先在windows上ping Ubuntu的ip地址,竟然发不了数据包 ...

  10. 三台CentOS 5 Linux LVS 的DR 模式http负载均衡安装步骤

    Linux负载均衡软件LVS(概念篇) 一. LVS简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目,它的官方站点是ww ...