我制作了一个自定义容器控制器,其中包括在两个控制器之间切换。当我将第一个控制器的视图添加到我的容器控制器时,Instrument 显示内存泄漏。我目前有以下实现:
。H
#import <UIKit/UIKit.h>
@interface SCLDefaultSwitchViewController : UIViewController
{
@protected
__weak UIViewController *_activeController;
}
@property (nonatomic, weak, readonly) UIViewController *activeController;
@property (nonatomic, weak) UIViewController *leftViewController;
@property (nonatomic, weak) UIViewController *rightViewController;
@结尾
米:
- (void)viewDidLoad
{
[super viewDidLoad];
// set-up content container view
self.view.autoresizesSubviews = YES;
[self performSegueWithIdentifier:@"left" sender:self];
NSAssert( (_leftViewController != nil) , @"left view controller shouldn't be nil!");
_activeController = _leftViewController;
[self addChildViewController:_activeController];
[self.view addSubview:_activeController.view]; // ---> memory leak here!!!!!
[_activeController didMoveToParentViewController:self];
[self performSegueWithIdentifier:@"right" sender:self];
NSAssert( (_rightViewController != nil) , @"right view controller shouldn't be nil!");
[self addChildViewController:_rightViewController];
[_rightViewController didMoveToParentViewController:self];
//[self performSelectorInBackground:@selector(performBackgroundTasks) withObject:nil];
}
我确保在两个视图之间切换时视图已从其父视图中正确删除:
- (void) didTapOnStartButton:(SCLSplashScreenViewController *)sender
{
//CGRect originFrame = _contentContainerView.bounds;
CGRect originFrame = self.view.bounds;
CGRect offsetFrame = originFrame;
offsetFrame.origin.x = originFrame.size.width;
UIViewController* toViewController = self.rightViewController;
UIViewController* fromViewController = self.leftViewController;
toViewController.view.frame = offsetFrame;
__weak SCLSwitchViewController *weakSelf = self;
[self transitionFromViewController:fromViewController
toViewController:toViewController
duration:.4f
options: UIViewAnimationOptionCurveEaseOut
animations:^{
weakSelf.rightViewController.view.frame = originFrame;
}
// remove the splash screen controller when the animation is finished and delete it: we don't need it anymore
completion:^(BOOL finished){
if (finished){
[weakSelf.leftViewController willMoveToParentViewController:nil];
[weakSelf.leftViewController.view removeFromSuperview];
[weakSelf.leftViewController removeFromParentViewController];
weakSelf.leftViewController = nil;
weakSelf.activeController = toViewController;
}
}];
}
我真的看不出它应该泄漏的任何充分理由:有什么想法吗?