iOS 開(kāi)發(fā):代理+KVO+通知+Block

iOS 委托代理(delegate)

本質(zhì):我提供一個(gè)我自己的委托(方法)。你如果要使用我這個(gè)方法,確認(rèn)代理(設(shè)置代理=self)就可以使用。

作用:

1:方法延續(xù)(引導(dǎo)頁(yè)動(dòng)畫結(jié)束后跳轉(zhuǎn)到主界面)(不需要帶傳遞參數(shù))

2:實(shí)現(xiàn)頁(yè)面?zhèn)髦?( TwoVc 傳值給 ?OneVc )(有傳遞參數(shù))

實(shí)現(xiàn)步驟

委托方

.h中

a.定義協(xié)議與方法

b.聲明委托變量

.m中

a.設(shè)置代理

b.通過(guò)委托變量調(diào)用委托方法

代理方:

a.遵循協(xié)議

b.實(shí)現(xiàn)委托方法

例子詳解

可以參考這個(gè)列子:聯(lián)動(dòng)效果實(shí)現(xiàn)

TwoViewController.h (引導(dǎo)頁(yè)/傳值頁(yè))

@protocol selfDelegate<NSObject>

- (void)passValue:(NSString *)values://帶參數(shù)的方法

- (void)doSomethingDelegate//不帶參數(shù)的方法

@end

@interface? TwoViewController : UIViewController ?

@property (nonatomic, assign) id<selfDelegate>SelfDelegate;

@end

TwoViewController.m

//引導(dǎo)頁(yè)消失的方法后面實(shí)現(xiàn)代理方法

if (self. SelfDelegate && [self. SelfDelegate respondsToSelector:@selector(doSomethingDelegate)])

{

? ? ?[self. SelfDelegate doSomethingDelegate];

}


AppDelegate.m

TwoViewController *guideview = [[TwoViewController alloc]init];

guideview.gotodelegate = self;//設(shè)置代理


- (void)doSomethingDelegate{

//實(shí)現(xiàn)代理方法:---->跳轉(zhuǎn)到首頁(yè)

帶參數(shù)的傳值同理:這里就不記錄了

可以參考鏈接:代理參數(shù)傳值


KVO傳值

添加

[***(誰(shuí)被觀察-改變) addObserver:self forKeyPath:@"isEdit" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

移除

- (void)dealloc {

[*** removeObserver:self forKeyPath:@"isEdit"];

}

回調(diào)

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

if ([keyPath isEqualToString:@"isEdit"]) {

}

}


通知傳值

<1>在我們需要時(shí)發(fā)送通知消息(發(fā)送:消息寫在userInfo中)

NSDictionary *dic =@{@"num":[NSString stringWithFormat:@"%lu",(unsigned long)self.detailArray.count]};

[[NSNotificationCenter defaultCenter] postNotificationName:@"shoppingCarNum" object:nil userInfo:dic];

<2>我們?cè)谛枰邮胀ㄖ牡胤阶?cè)觀察者(注意移除)

獲取通知中心單例對(duì)象。添加當(dāng)前類對(duì)象為一個(gè)觀察者,name和object設(shè)置為nil,表示接收一切通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notice:) name:@"shoppingCarNum" object:nil];

我們可以在回調(diào)的函數(shù)中取到userInfo內(nèi)容,如下:

-(void)notice:(NSNotification *)notification{

self.label.text=[notification.userInfo objectForKey:@“1”];

}

消息發(fā)送完,要移除掉

- (void)dealloc ?{

//移除所有通知

[[NSNotificationCenter?defaultCenter]?removeObserver:self];

//移除指定通知

[[NSNotificationCenter defaultCenter]removeObserver:self name:@"labelTextNotification" object:nil];

[super dealloc];

}


Block傳值

<1>聲明 cell.h

@property (nonatomic,copy)? void(^shopCartBlock)(UIImageView *imageView,NSInteger selectRow);

也可以這么寫B(tài)lock

typedef void (^SelfBlock)(UIImageView *imageView,NSInteger selectRow);

@property (nonatomic,copy) SelfBlock shopCartBlock;

<2>實(shí)現(xiàn) ?cell.m

if (self.shopCartBlock) {

self.shopCartBlock(self.CommodityImage,path.row);

}

<3>調(diào)用 ?**.m

cell.shopCartBlock = ^(UIImageView *imageView,NSInteger selectRow) { ? }

學(xué)無(wú)止境,做個(gè)記錄

2017-02-5-SXH

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容