日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OC-通知+Block

發布時間:2024/4/14 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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修飾符即可

轉載于:https://www.cnblogs.com/GJ-ios/p/5334070.html

總結

以上是生活随笔為你收集整理的OC-通知+Block的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。