2013-05-03 97 views
0

我有模態UIViewController。 在收到低內存警告時,我想關閉該模式。 爲此,我寫了下面的代碼 -iOS - 停止Modal UIViewController呈現

- (void)didReceiveMemoryWarning 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    [super didReceiveMemoryWarning]; 
} 

這在大多數情況下的罰款。但是,當我初始化了視圖控制器,但還沒有在屏幕上顯示它,並且當時發生了內存警告,那麼代碼執行繼續,模式不會因爲它尚未呈現而被解散,以及當我執行達到它呈現的地步,模態仍然顯示出來。

我該如何着手處理這種情況並防止出現模態?如果有什麼不清楚的地方,請告訴我 - 我會盡力更好地解釋它。

+0

你不希望你收到內存不足警告後一旦目前的模式來看? – 2013-05-03 10:32:01

回答

0

你可以按照下面的代碼檢查結果...

- (void)didReceiveMemoryWarning 
{ 
    if(!self.myVC) 
    { 
    [self presentViewController:myVC animated:NO 
    completion: 
       { [self dismissViewControllerAnimated:NO completion:nil];}]; 
    } 
    else 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
0

當您收到內存警告,如果你的模式是沒有出現,那麼你可以釋放模式視圖控制器,它等同於零。您可以在顯示模態視圖之後設置一個布爾值,並利用didReceiveMemoryWarning中的值來消除已經顯示的模態視圖。

-(void)didReceiveMemoryWarning 
{ 
    if(!_isModalPresnted) 
    { 
     [_modalViewController release] 
     _modalViewController = nil; 
    } 
    else 
    { 
     [self dismissViewControllerAnimated:YES completion:nil]; 
    } 
} 

因此,當你執行到達那裏得到呈現,你可以有一個檢查

if(_modalViewController) 
{ 
     _isModalPresnted = YES; 
    [self presentViewController:_modalViewController animated:YES completion:nil]; 

} 
相關問題