如何从视图中删除子视图并释放内存

时间:2014-11-01 10:58:47

标签: ios objective-c memory-management

你好我是新的 iOS内存管理我的问题是当我从视图中删除视图子视图时间内存没有减少。在我,我试图使可重用的视图。有3个视图是主显示视图。当使用交换时,所有树视图位置都是更改,子视图存储在NSMutableArray中。但是当我删除3个视图子视图时,它不释放内存。我的代码在arc=YES,我的代码是Bellow

int Position = 0;
- (void)viewDidLoad
{
    [super viewDidLoad];

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    // Setting the swipe direction.
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

    // Adding the swipe gesture on image view
    [self.view addGestureRecognizer:swipeLeft];
    [self.view addGestureRecognizer:swipeRight];
    Position = 0;

    Arr_view = [[NSMutableArray alloc] initWithObjects:View_D1,View_D2,View_D3,View_D4,View_D5,View_D6,View_D7,View_D8,View_D9,View_D10,View_D11,View_D12,View_D13,View_D14 nil];

    [View_D1 setFrame:CGRectMake(0, 0, 320, 568)];
    [View_D2 setFrame:CGRectMake(320, 0, 320, 568)];
    [View_D3 setFrame:CGRectMake(640, 0, 320, 568)];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        Position++;
    }

    if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
    {
        Position--;
    }
    if (Position>=0 && Position<Arr_view.count)
    {
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(ChangePosition) userInfo:nil repeats:NO];
    }
    else {
        if (Position<0) {
            Position=0;
        }
        else {
            Position = Arr_view.count-1;
        }
    }
}
-(void)ChangePosition
{
    if (Position%3 == 0) {
        [View_D1 addSubview:(UIView *)[Arr_view objectAtIndex:Position]];

        [View_D1 setFrame:CGRectMake(0, 0, 320, 568)];
        [View_D2 setFrame:CGRectMake(320, 0, 320, 568)];
        [View_D3 setFrame:CGRectMake(640, 0, 320, 568)];

        [[View_D3 subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [[View_D2 subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

    }
    else if (Position%3 == 1) {
        [View_D2 addSubview:(UIView *)[Arr_view objectAtIndex:Position]];

        [View_D1 setFrame:CGRectMake(640, 0, 320, 568)];
        [View_D2 setFrame:CGRectMake(0, 0, 320, 568)];
        [View_D3 setFrame:CGRectMake(320, 0, 320, 568)];

        [[View_D1 subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [[View_D3 subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    else  {
        [View_D3 addSubview:(UIView *)[Arr_view objectAtIndex:Position]];

        [View_D1 setFrame:CGRectMake(320, 0, 320, 568)];
        [View_D2 setFrame:CGRectMake(640, 0, 320, 568)];
        [View_D3 setFrame:CGRectMake(0, 0, 320, 568)];

        [[View_D1 subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [[View_D2 subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
}

2 个答案:

答案 0 :(得分:0)

您的代码看起来不错。如果您正在使用arc,则无需担心释放内存。因为在弧中您无法直接控制何时释放对象,所以ARC会在释放对象后立即解析您的代码并插入释放语句。这可以立即释放内存以进行新的分配。请参阅this

答案 1 :(得分:0)

任何NSObject子类都将被释放,当它保持计数变为0时,你对viewController中的视图有强烈的引用,在这种情况下,它足以从superview中删除该视图并将其引用设置为nil。但是,如果您需要重用此视图,则无需取消分配,并且必须对其进行强引用。