2016-09-22 69 views
0

我一直在嘗試使用塊數據傳遞迴用塊而不是委託

數據

secondViewController .h文件中

@property (nonatomic, copy)void(^myBlock)(NSString *); 

呼叫發送的數據落後

聲明塊傳遞塊無論你需要從.m文件傳遞數據secondViewController

myBlock(@"this will displayed in firstViewController"); 

3.進口在firstViewController .m文件.h文件中上方,並定義塊作爲

secondViewController *ref =[[secondViewController alloc ]init]; 
ref.myBlock =^void(NSString *data) 
{ 
self.labelOffirstviewcontroller=data; 
}; 

同時運行時我得到EXC壞訪問錯誤誰能排序的問題呢?

+0

你在錯誤的方式定義關閉。我們沒有定義關閉屬性 – Wolverine

+0

然後你可以解釋如何定義它? @Wolverine –

+0

typealias ClosureType =(ParameterTypes) - >(ReturnType) – Wolverine

回答

0

這裏[[secondViewController alloc ]init];你正在創建一個視圖控制器的新實例,所以你可以設置它的塊,但它不會被使用和被破壞。您需要使用prepareForSegue或類似,具體取決於您如何顯示VC,以便將塊設置爲正確的實例。

VC2通常應該在調用之前檢查塊是否爲零。

該屬性也應該強大,而不是複製。無論如何,ARC做正確的事情。

0

我找到了一種傳遞數據的方法。

secondViewController *ref =[[secondViewController alloc ]init]; 
ref.myBlock =^void(NSString *data) 
{ 
self.labelOffirstviewcontroller=data; 
}; 

應該給這個代碼,我們推的ViewController