在iOS应用程序中泄漏内存

时间:2014-07-03 15:25:56

标签: ios objective-c xcode memory

我的NavigationController有两个控制器。当我去第二个控制器时,一切都很好。但是,当我返回第一个控制器然后再次返回第二个控制器时,使用的内存量会增加(每次第二个控制器再次加载时,内存会永久增长)。在处理重复加载的viewDidLoad方法中,_loadServicesView_complexTableView_selectTypeCarView等对象设置为nil,然后再次创建。为什么在从第二个控制器转换到第一个控制器时这些对象是否被销毁?此外,如果它们没有被销毁,为什么它们被设置为零?

这里是与第二个控制器相关的代码:

@interface WCselectTypeViewController () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) WCloadServices       *loadServices; 

@property (strong, nonatomic) WCloadView           *loadServicesView;

@property (strong, nonatomic) WCselectTypeView     *selectTypeView;

@property (strong, nonatomic) WCcomplexTableView   *complexTableView;

@end

@implementation WCselectTypeViewController


- (void)loadView
{
     [super loadView];
}

- (void)viewDidLoad
{
     [super viewDidLoad];

     if (!_loadServicesView)
     {
         _loadServicesView = [[WCloadView alloc] init];
         [_loadServicesView setDelegate:self];
         [self.view addSubview:_loadServicesView];
     }

     if (!_complexTableView)
     {
         _complexTableView = [[WCcomplexTableView alloc] initWithViewConfig:[WCappConfig getViewConfig]];
         _complexTableView.alpha = 0;
         _complexTableView.delegate = self;
         _complexTableView.dataSource = self;
         [self.view addSubview:_complexTableView];
    }

     if (!_selectTypeView)
     {
         _selectTypeView = [[WCselectTypeCarView alloc] initWithViewConfig:[WCappConfig getViewConfig] andType:[[WCorderStored sharedStore] getType]];
         [_selectTypeCarView setDelegate:self];
         [self.view addSubview:_selectTypeView];
     }
 }

1 个答案:

答案 0 :(得分:0)

我认为这与我所经历的相同(错误/)行为有关。改变VC将把它们放在堆栈上;内存应该减少,如果你使用后箭头,不是吗?

我使用了PageViewController而不是导航控制器。

这是我的工作解决方案:

- (void)viewDidLoad
{
    [super viewDidLoad];


    // Create the data model
    _pageTitles = @[@"1", @"2", @"3", @"4", @"5"];
//    _pageImages = @[@"page1.png", @"page2.png", @"page3.png", @"page4.png"];

    // Create page view controller

    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
    self.pageViewController.dataSource = self;

    PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    // Change the size of page view controller
    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);

    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];

}

- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{

    if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
        return nil;
    }
    // Create a new view controller and pass suitable data.


//    pageContentViewController.imageFile = self.pageImages[index];

    PageContentViewController *pageContentViewController;
    if (index == 0){
        pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailView1"];
        pageContentViewController.pageIndex = index;
    }
    else if (index == 1){
        pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailView2"];
        pageContentViewController.pageIndex = index;
    }
    else if (index == 2){
        pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailView3"];
        pageContentViewController.pageIndex = index;
    }
    else if (index == 3){
        pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailView4"];
        pageContentViewController.pageIndex = index;
    }
    else if (index == 4){
        pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailView4"];
        pageContentViewController.pageIndex = index;
    }

    return pageContentViewController;

}