2015-10-07 24 views
0

在我的代碼中,我正在無限地調用viewWillLayoutSubviews方法。在viewWillLayoutSubviews,我重裝這將導致嚴重的內存泄露表導致到屏幕上凍結我的iPad 2致電查看WillLayoutSubviews infintely其中導致屏幕凍結和內存警告

- (void)viewWillLayoutSubviews { 
    [super viewWillLayoutSubviews]; 

    if ([APP_DELEGATE popOverIsOpened]) { 

    [socalMedia dismissView]; 
    [self performSelectorInBackground:@selector(exiBack) withObject:nil]; 
    } 
    [self.pTableView reloadData]; 
    if (([[UIApplication sharedApplication] statusBarOrientation] == 
     UIDeviceOrientationPortrait) || 
     ([[UIDevice currentDevice] orientation] == 
     UIDeviceOrientationPortraitUpsideDown)) { 
    _pView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)]; 
    webView.frame = CGRectMake(0, 64, 768, 1024 - 64); 
    } else { 

    _pView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]; 
    webView.frame = CGRectMake(0, 64, 1024, 768 - 64); 
    } 

    CGRect frm = self.navigationController.view.frame; 
    frm.origin.y = 0; 
    self.navigationController.view.frame = frm; 

} 

在什麼情況下,所謂的viewWillLayoutSubviews方法?它是自動調用還是當方向改變或什麼時候調用?

回答

1

當View調整大小(此處將frm設置爲self.navigationController.view.frame)並由其父項定位時,viewWillLayoutSubviews方法會被調用。

+0

如果viewWillLayoutSubviews方法在視圖調整大小時調用,那麼爲什麼它會在我的代碼中進入無限調用!最初當渲染視圖時,將會有一個調用,但不是無限的。 – pkc456

+1

嘗試評論此「CGRect frm = self.navigationController.view.frame; frm.origin.y = 0; self.navigationController.view.frame = frm;」 – nikhilgohil11

0

你最好做一件事情采取布爾,並將其設置爲TRUE按鈕按下並檢查layoutSubviews並操作它是否爲真或假。你得到它我正在做的事情。它會確保layoutSubView不叫無窮大,這將有助於應用程序不凍結和沒有內存警告。

+0

感謝Prabhjot回答。你的解決方案可能會工作,但我想知道這個viewWillLayoutSubviews方法調用時的確切原因。是什麼導致一次又一次佈置子視圖。 – pkc456

+0

hi @ pkc456 viewWillLayoutSubviews方法在視圖調整大小並由父級定位後調用。所以它會導致viewWillLayoutSubviews方法被稱爲無限次。 –

+0

以及@ pkc456鑑於viewWillLayoutSubviews在控制器視圖中的邊界發生變化時被調用,所以不保證它只會被調用一次。每當發生旋轉時都會調用它 –