UIViewController在popup时不会从内存中释放

我有一个UINavigationController ,我试图从内存释放每个UIViewController一旦另一个在堆栈顶部。 我将UINavigationControllerviewControllers属性分配给新的UIViewController ,然后popup。 这样我总是只有一个UIViewController在堆栈中。 但是,每当我创build一个新的UIViewController时,内存不断增加。 Dealloc被调用,但内存使用保持不变。

您可以在这里下载示例项目

FirstViewController.h

 #import "SecondViewController.h" @interface FirstViewController : UIViewController -(IBAction)goToSecond:(id)sender; @end 

FirstViewController.m

 #import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%@", self.navigationController.viewControllers); } -(void)goToSecond:(id)sender{ SecondViewController *secondVC = [[SecondViewController alloc]init]; [self.navigationController setViewControllers:@[secondVC]]; [self.navigationController popViewControllerAnimated:NO]; } -(void)dealloc{ NSLog(@"FirstVC dealloc"); } @end 

SecondViewController.h

 #import "FirstViewController.h" @interface SecondViewController : UIViewController -(IBAction)goToFirst:(id)sender; @end 

SecondViewController.m

 #import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%@", self.navigationController.viewControllers); } -(void)goToFirst:(id)sender{ FirstViewController *firstVC = [[FirstViewController alloc]init]; [self.navigationController setViewControllers:@[firstVC]]; [self.navigationController popViewControllerAnimated:NO]; } -(void)dealloc{ NSLog(@"SecondVC dealloc"); } @end 

导航控制器不应按照您的意图使用。 你应该调用pushViewControllerpopViewController来呈现/closures你的viewControllers。 如果您遇到内存问题,请尝试释放didReceiveMemoryWarningcallback中的内存

我不确定uinavigationcontroller的好处,但无论如何,你可以添加这个片段在你的uiviewcontrollers的.m

 -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; if (self.navigationController.viewControllers.count > 1) { NSMutableArray *newViewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers]; [newViewControllers removeObject:[controllers objectAtIndex:1]]; self.navigationController.viewControllers = newViewControllers; } } 

而不是

 [self.navigationController setViewControllers:@[firstVC]]; [self.navigationController popViewControllerAnimated:NO]; 

你可以设置

 [self.navigationController pushViewController:[[FirstViewController alloc] init] animated:NO]; 

你正在使用popup进一步,但你需要使用push如果你想要去下一个ViewController

 -(void)goToSecond:(id)sender{ SecondViewController *secondVC = [[SecondViewController alloc]init]; [ self.navigationController pushViewController:secondVC animated:YES]; } 

SecondViewController中回到你的FirstViewController你应该使用pop

 -(void)backToController { [self.navigationController popViewControllerAnimated:YES]; } 

在你的情况

 -(void)goToFirst:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } 

我上传了一个简单的应用程序GitHub,我在我的评论中说的假导航栏,希望它有助于您的需求: https : //github.com/yosihashamen/HelpersApps

必须始终保持“BaseViewController”活着。

我的意思是这样的。

在你的FirstViewController使用presentViewController而不是向presentViewController添加一个新的UINavigationController

 -(void)goToSecond:(id)sender{ SecondViewController *secondVC = [[SecondViewController alloc]init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:secondVC]; [self presentViewController:nav animated:YES completion:NIL]; } 

SecondViewController添加一个UIBarButtonItemNavigation bar

 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:nil action:@selector(goToFirst:)]; self.navigationItem.backBarButtonItem = backButton; 

并执行解雇方法。

 -(void)goToFirst:(id)sender { [self dismissViewControllerAnimated:YES completion:NULL]; } 

试试setViewControllers:animated:

这允许你显式地设置UINavigationController栈上的视图控制器,就像你正在做的那样,它将自动处理导航animation而不必调用popViewControllerAnimated:

如果你有一个多视图旅程,你需要摆脱迄今为止显示的屏幕,但保持导航animation(例如,启动时的应用程序演示),或者如果你想轻松地推动多个视图控制器导航堆栈一次。

苹果文件在这里: https : //developer.apple.com/library/ios/documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html#jumpTo_21