[英]custom iOS 7 UIViewController transition retain cycle
我正在我的应用程序中创建自定义转换并遇到两个问题。 如果我设置视图控制器来处理UIViewControllerAnimatedTransitioning
和UIViewControllerTransitioningDelegate
我遇到了我的视图控制器永远不会被释放的问题。 具体来说,这会创建保留:
self.transitioningDelegate = self;
如果我不这样做,并将UIViewControllerAnimatedTransitioning
和UIViewControllerTransitioningDelegate
放在一个名为TransitionController的单独的NSObject
,并将其设置为:
self.transitioningDelegate = [[TransitionController alloc] init];
UIViewController
被释放 ,但我在TransitionController对象上得到了内存泄漏。 现在有人,我做错了吗?
我遇到了同样的问题并且能够解决它。
自定义转换API没有很好的文档记录,并且需要大量的试验和错误才能使一切正常。
让我向您介绍一下如何在没有任何内存问题的情况下使其正常工作:
VCA =想要以模态方式呈现VCB的视图控制器
VCB =模态呈现的视图控制器(由VCA提供)
TC =执行自定义动画的自定义转换控制器对象。
符合“ UIViewControllerAnimatedTransitioning ”的NSObject子类。
将在TD内实例化。
TD =为系统提供转换控制器的自定义转换委托对象。 符合“ UIViewControllerTransitioningDelegate ”的NSObject子类
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];
self = VCA的一个实例
myModalViewController =前面介绍的模块化VCB实例
[self dismissViewControllerAnimated:YES
completion:^{
[self.myModalViewController setTransitioningDelegate: nil]; // DO THIS!!!!
self.myModalViewController = nil;
}];
我希望这有帮助。 它确实对我有用。
在我来说,我有自己( UIViewController
)拿着一个自定义的一个实例UIViewController
(可以称之为mViewController
)和self
是transitioningDelegate
显示/驳回的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.