一些簡單實用的block使用方法难裆。
從A頁面跳轉(zhuǎn)到B頁面哨查,退出B頁面后再把消息會傳給A。
基本流程:A -> B,B -> A
在B頁面的操作
- 在B頁面創(chuàng)建block,輸入typ惰蜜,會顯示出
typedef <#returnType#>(^<#name#>)(<#arguments#>);
-
#returnType#
是返回的類型,默認(rèn)是void
-
#name#
是block的名字 -
#arguments#
是回調(diào)的參數(shù)受神,比如回調(diào)字符串抛猖,就是NSString *string
在這里,我是用typedef void (^ReturnValueBlock) (QTMResModel *model);
-
- 然后創(chuàng)建方法:
@property (nonatomic, copy) ReturnValueBlock returnValueBlock;
- 在B頁面使用的時候:
WS(weakself);
if (weakself.returnValueBlock) {
將自己的值傳出去,完成傳值
weakself.returnValueBlock(model);
}
在A頁面的操作
A.returnValueBlock = ^(QTMResModel *model){
self.model = model;
};
這樣就完成了簡單思的頁面?zhèn)髦怠?/p>
最后
大多數(shù)時候财著,用這個簡單的傳值就能滿足我們大部分的需求养交,但是要注意在使用時的內(nèi)存泄漏的問題