2014-11-01 80 views
0

你好,我是在iOS的內存管理新我的問題是,當我從認爲時間內存不減少刪除視圖子視圖。在我的嘗試做出可重用的觀點。有3個視圖是主顯示視圖。當使用swap那個時候所有的樹視圖位置都是變化的,並且子視圖存放在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)]; 
    } 
} 

回答

0

您的代碼只看起來不錯。如果你使用弧,那麼不需要擔心釋放內存。由於電弧不能直接對象被釋放時控制,ARC分析你的代碼,並插入只要物體可以釋放釋放語句。這可以立即釋放新分配的內存。參考this

+0

感謝您的重播 – 2014-11-01 13:23:10

0

任何NSObject子類將被釋放,當它保留count變爲0時,你有很強的引用你在viewController中查看,在這種情況下它足以從superview中移除該視圖並將其引用設置爲nil。但是如果你需要重用這個視圖,你不需要重新分配它,並且必須留下強烈的參考。

相關問題