OC-通知+Block
===================================
一.通知(NSNotification)
NSNotification 通知类,这个类中有 NSNotificationCenter 通知中心类
NSNotificationCenter* notification = [NSNotificationCenter defaultCenter];
添加了一个监听事件,其中,run1 则是触发的事件方法,@“run”是通知的名字
[notification addObserver:self selector:@selector(run1) name:@"run" object:nil];
多次调用发送通知的方法,会触发多次相应的响应方法(run1)
[notification postNotificationName:@"run" object:nil];
删除通知,如果想删除通知,就可以调用removeObserver这个方法
[notification removeObserver:self name:@"run" object:nil];
【注】通知用的时候要添加通知,不用的时候一定要删除通知,因为如果不删除,这个通知一直存在
二.【代理和通知对比】
代理:小明->小刚->小红->小李;结果:小李->小红->小刚->小明
通知:小明注册了通知;结果:小刚、小红、小李都可以给小明发送消息;
三.通知的注意事项
+(void)test
{
xiaoming *xm = [[xiaoming alloc]init];
[xm test1];
【注】不可以在类方法中添加监听方法,这样会导致程序崩溃
NSNotificationCenter* notification = [NSNotificationCenter defaultCenter];
[notification addObserver:self selector:@selector(run) name:@"run" object:nil];
[xiaogang xgTest];
}
-(void)test1
{
NSNotificationCenter* notification = [NSNotificationCenter defaultCenter];
[notification addObserver:self selector:@selector(run) name:@"run" object:nil];
【注】添加监听事件多次,发送消息时会触发多次run方法
[notification addObserver:self selector:@selector(run) name:@"run" object:nil];
[notification addObserver:self selector:@selector(run) name:@"run" object:nil];
[notification addObserver:self selector:@selector(run) name:@"run" object:nil];
【注】删除监听,会删除所有对应的name的监听
[notification removeObserver:self name:@"run" object:nil];
【注】删除监听,会删除所有对应的name的监听,object后面的参数应根据addObserver方法中的参数来
[notification removeObserver:self name:@"run" object:@""];
[notification addObserver:self selector:@selector(run) name:@"run" object:nil];
[xiaogang xgTest];
}
一.认识block================================================================
block又称为代码块,它是^符号开头的方法;一般用于多线程、网络通信。苹果公司从ios4开始主推block语法
block实体形式如下:
^(传入的参数列表){行为主体(具体的代码实现)}
c语言中声明了一个指针函数
void (* cFunc)(void);
oc中block 跟指针函数很像
写了一个block变量ocFunc
void(^ ocFunc)(void);
一.不带参数的block-------------------------------------------------------------------------------------------------------------------
【注】block语法,先执行{}外面的语法,只有调用block函数的时候,才会执行内部
实现了一个block函数
^(传入的参数列表){行为主体(具体的代码实现)}
【注】block函数是以;结尾
ocFunc=^(void)
{
NSLog(@"in blocks");
};
NSLog(@"befor blocks");
block函数的调用
ocFunc();
NSLog(@"after blocks");
二.带参数的block---------------------------------------------------------------------------------------------
int 返回值类型;myblock1 block函数名称;int a,int b是形参;^(int a,int b){};是行为主体
int (^ myblock1)(int a,int b)=^(int a,int b)
{
return a+b;
};
block函数的调用
int result1 = myblock1(10,20);
NSLog(@"result1 = %d",result1);
一个函数中无法包含另外一个函数,block应运而生了
func(10,20);
int b = 8;
int (^myblock2)(int a) = ^(int a)
{
return b+a;
};
int result2 = myblock2(5);
NSLog(@"rusult2 = %d",result2);
myBlock myblock3 = ^(int a,int b)
{
return a+b;
};
int result3 = myblock3(90,8);
NSLog(@"rusult3 = %d",result3);
【注】如果要在block内部对外部变量进行修改,则外部变量需要加__block修饰符(有2条下划线)
__block int sum;
void(^myblock4)(int a,int b) = ^(int a,int b)
{
sum = a +b;
};
myblock4(4,5);
NSLog(@"sum = %d",sum);
比较有意思的事情
A这个值会copy一份,block内部的操作是copy的这一部分,所以,外部无论如何对这个A进行修改,block内部都是不变的
int A = 8;
int(^myblock5)(int ) = ^(int a)
{
return A + a;
};
A = 5;
int result4 = myblock5(3);
NSLog(@"result4 = %d",result4);
【注】需要注意的是,这里copy的值是一个变量的值,如果是一个记忆体的位置(地址),也就说,就是这个变量的指针的话,它的值在block内部会被改变
NSMutableArray* array = [[NSMutableArray alloc]initWithObjects:@"one",@"two",@"three", nil];
void(^myblock6)(void) = ^(void)
{
[array removeLastObject];
};
在第0个位置插入字符串@“0”;
[array insertObject:@"0" atIndex:0];
myblock6();
NSLog(@"array = %@",array);
对sum进行赋值,发现sum值被修改了
void(^myblock7)(void) = ^(void)
{
sum = 6;
};
myblock7();
NSLog(@"sum = %d",sum);
另外一个比较有意思的事情
static int B = 8;
int (^myblock8)(int) = ^ (int a)
{
return B+a;
};
B = 5;
int result5 = myblock8(3);
NSLog(@"result5 = %d",result5);
static int B = 8;
int (^myblock8)(int) = ^ (int a)
{
B = 5;
return B+a;
};
int result5 = myblock8(3);
NSLog(@"result5 = %d",result5);
[注]如果想把一个变量参与到block中运算修改,加一个static修饰符即可
OC-通知+Block的更多相关文章
- OC语言BLOCK和协议
OC语言BLOCK和协议 一.BOLCK (一)简介 BLOCK是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. BOLCK和函数的相似 ...
- 李洪强iOS开发之OC语言BLOCK和协议
OC语言BLOCK和协议 一.BOLCK (一)简介 BLOCK是什么? 苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. BOLCK和函数的相 ...
- OC语言-block and delegate
参考博客 OC语言BLOCK和协议 iOS Block iOS Block循环引用精讲 iOS之轻松上手block 深入浅出Block的方方面面 Block apple官方参考 1.定义一个block ...
- OC语言Block 续
OC语言 Block 转载:http://blog.csdn.net/weidfyr/article/details/48138167 1.Block对象中的变量行为 结论: 在block代码块内部可 ...
- OC语言Block
OC语言Block 一.Block (一)简介 Block是什么?苹果推荐的比较特殊的数据类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. Block和 ...
- 61 (OC)* 代理 block 通知 代理 kvo
1.从源头上理解和区别block和delegate delegate运行成本低,block的运行成本高. block出栈需要将使用的数据从栈内存拷贝到堆内存,当然对象的话就是加计数,使用完或者bloc ...
- iOS OC语言: Block底层实现原理
先来简单介绍一下BlockBlock是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,Block可以在任何时候执行. Block和函数的相似性:(1)可以保存代码(2) ...
- 页面之间传值方式的总结,五种方式,通知,block,代理,单例,NSUERDEFALUT,
首先代码拿上 1:单例 2:通知 3:代理 4:block方法 5:NSUSERDEFAULT(沙盒文件) 先是单例类: .h文件 @interface DataSource : NSObject @ ...
- OC基础--block
block与函数类似:1.可以保存代码 2.又返回值 3.有形参 4.调用方式一样 block的标志:^ 一.定义一个无参无返回值的block void (^myBlock)();--1.void 代 ...
- OC:Block语法、Block使用、Block实现数组排序
Block //定义一个求两个数最大值函数 int maxValue (int ,int); //函数的实现 int maxValue (int a, int b){ return a > b ...
随机推荐
- python中math常用函数
python中math的使用 import math #先导入math包 1 三角函数 print math.pi #打印pi的值 3.14159265359 print math.radians(1 ...
- C语言高级宏技巧
特殊符号#.## (1)# When you put a # before an argument in a preprocessor macro, the preprocessor turns t ...
- NHibernate & INotifyPropertyChanged
One of the things that make NHibernate easy to use is that it fully support the POCO model. But one ...
- clipboard
我们在网页上放置一个复制按钮,主要用来方便用户复制链接之类的复杂文本,以往的做法是,通过JS依靠Flash,甚至借助jQuery庞大的js库来实现文本复制到剪贴板的.今天我要给大家介绍的是一款极现代的 ...
- iOS 调整图片尺寸,告诉你的UI,别问我尺寸!我要最大的
如果UI问你“这个图片你要多大尺寸的?”,你该怎么回答呢? 为了不让图片因拉伸或压缩而变形或模糊,请精确告诉UI图片的比例,至于图片的分辨率,不必要要求太严,但是最低要求是要让最大的屏幕不会出现前面描 ...
- 用VS2013编译FFMPEG232
http://blog.csdn.net/finewind/article/details/38854517 如果只是拿来使用,网上有现成的SDK.但我是想深入研究FFMPEG代码,又不熟悉Linux ...
- shell检查网络出现异常、僵尸进程、内存过低后,自动重启
#!/bin/bash while : do neterror=$(/bin/netstat -a | grep -cw "CLOSE_WAIT") echo "get ...
- Linux下MySQL 5.6.24的编译安装与部署
MySQL 5.6正式版发布了,相对于5.5版本作出了不少改进,其源码安装配置方式也有所变化,本文根据实际操作,不断尝试,精确还原了安装的具体步骤. 在Linux下安装MySQL前,先确认卸载系统自带 ...
- [POI2013]BAJ-Bytecomputer
题目描述 A sequence of integers from the set is given. The bytecomputer is a device that allows the foll ...
- 出入Spring boot(六)数据访问
Spring Data提供了使用统一的API进行数据访问操作,这是Spring通过提供Spring DataCommons项目来实现的,它是Spring data的依赖Spring Data Comm ...