簡體   English   中英

在塊完成時調用presentModalViewController

[英]call presentModalViewController in block completion

在我的代碼中,我正在使用此類:

https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets

當我以這種方式在對話框視圖中按下按鈕時,我想調用presentModalViewController:

BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Example" message:@"Text"];
[alert setDestructiveButtonWithTitle:@"Ok" block:^{
FirstView *firstView = [[FirstView alloc] initWithNibName:@"FirstView" bundle:nil];
[self presentModalViewController:firstView animated:YES];
}];
[alert show];

但是應用程序凍結,並在一分鍾后打開視圖,所以我的問題是如何在區塊完成中呈現emodalviewcontroller?

實際上有一些錯誤,首先,當您實例化某個塊內的某個類/變量時,它們會響應並在該塊內具有生命周期。 因此,您的代碼塊在另一個線程中運行,這就是原因,通過這種方式,您可能會導致保留周期,這對於您的應用程序來說是非常糟糕的情況,請更改如下所示:

BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Example" message:@"Text"];
FirstView *firstView = [[FirstView alloc] initWithNibName:@"FirstView" bundle:nil];
__weak typeof(self) weakSelf = self;
[alert setDestructiveButtonWithTitle:@"Ok" block:^{

[weakSelf presentModalViewController: weakSelf.firstView animated:YES];
}];
[alert show];

為此,使用__weak typeof我們創建了一個弱引用,該阻止阻止塊創建強引用循環*(易於閱讀),現在我們確保將使用為當前新模態視圖實例化的視圖。

任何疑問都可以免費提出:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM