簡體   English   中英

自定義iOS 7 UIViewController轉換保留周期

[英]custom iOS 7 UIViewController transition retain cycle

我正在我的應用程序中創建自定義轉換並遇到兩個問題。 如果我設置視圖控制器來處理UIViewControllerAnimatedTransitioningUIViewControllerTransitioningDelegate我遇到了我的視圖控制器永遠不會被釋放的問題。 具體來說,這會創建保留:

self.transitioningDelegate = self;

如果我不這樣做,並將UIViewControllerAnimatedTransitioningUIViewControllerTransitioningDelegate放在一個名為TransitionController的單獨的NSObject ,並將其設置為:

self.transitioningDelegate = [[TransitionController alloc] init];

UIViewController釋放 ,但我在TransitionController對象上得到了內存泄漏。 現在有人,我做錯了嗎?

我遇到了同樣的問題並且能夠解決它。
自定義轉換API沒有很好的文檔記錄,並且需要大量的試驗和錯誤才能使一切正常。

讓我向您介紹一下如何在沒有任何內存問題的情況下使其正常工作:

以下是球員:

VCA =想要以模態方式呈現VCB的視圖控制器
VCB =模態呈現的視圖控制器(由VCA提供)

TC =執行自定義動畫的自定義轉換控制器對象。
符合“ UIViewControllerAnimatedTransitioning ”的NSObject子類。
將在TD內實例化。

TD =為系統提供轉換控制器的自定義轉換委托對象。 符合“ UIViewControllerTransitioningDelegate ”的NSObject子類

現在讓我們介紹VCB的一個實例

self = VCA的一個實例
myModalViewController =是self的強大屬性

self.myModalViewController = [[VCB alloc] init];

[self.myModalViewController setModalPresentationStyle: UIModalPresentationCustom];
[self.myModalViewController setTransitioningDelegate: [[TD alloc] init]];
[self presentViewController: self.myModalViewController
                   animated:YES 
                 completion:NULL];

稍后,VCB要求VCA被解雇

self = VCA的一個實例
myModalViewController =前面介紹的模塊化VCB實例

[self dismissViewControllerAnimated:YES 
                         completion:^{
             [self.myModalViewController setTransitioningDelegate: nil]; // DO THIS!!!! 
             self.myModalViewController = nil;
        }];



我希望這有幫助。 它確實對我有用。

在我來說,我有自己( UIViewController )拿着一個自定義的一個實例UIViewController (可以稱之為mViewController )和selftransitioningDelegate顯示/駁回的mViewConroller 我避免保留周期的解決方案是在mViewController.m內調用它:

-(void)viewDidDisappear:(BOOL)animated {
    self.transitioningDelegate = nil;
    [super viewDidDisappear:animated];
}

工作就像一個魅力(:

在第二次嘗試中,您正在分配TransitionController實例,它將永遠不會被釋放(因為沒有人對它有引用)。 對象永遠不應該在Objective-C中保留它的委托,所以你需要引用你的ViewController以及它代碼的另一個delegate

在iOS7中轉換完成 ,來自VC的UIViewControllerAnimatedTransitioning對象保留(這在iOS8中不會發生),如果轉換對象在屬性中存儲任何內容,則可能導致內存泄漏。 這讓我在過去,需要注意的事情。

在我的例子中,導致保留我呈現的視圖控制器的原因是我將錯誤的布爾值傳遞給動畫的完成塊。

[transitionContext completeTransition:transitionContext.transitionWasCancelled];

應該是這樣的:

BOOL successful = transitionContext.transitionWasCancelled == NO;
[transitionContext completeTransition:successful];

將代碼拆分為兩行有助於提高可讀性。

暫無
暫無

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

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