2014-05-21 42 views
0

我只是不明白我在做什麼錯。我有2個不相關的視圖控制器,我通過將視圖控制器拖動到屏幕創建了storyboard。每個在故事板中都有它的名稱標識符,我用它們的名字在它們之間進行替換。如何正確替換視圖控制器

問題是,在第一次更換後,我得到了與現實無關的數十條內存警告,因爲我的內存消耗約爲59M,並保持這種狀態。當我從A切換到B並且回到A時,有時它會由於「記憶湖」而失敗(59M !!)。

當我更換視圖控制器時,我做錯了什麼?

- (IBAction)goMatrix:(id)sender 
{ 
    UIViewController *mainV=[self.storyboard instantiateViewControllerWithIdentifier:@"MatrixView"]; 
    mainV.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentViewController:mainV animated:YES completion:^(void) 
    { 
     scroller.delegate=nil; 
     scroller=nil; 
     [imagesBuffer removeAllObjects]; 
     [scroller removeFromSuperview]; 
     [stripView removeFromSuperview]; 


    }]; 

} 

我對他們兩人與圖像的滾動視圖,我剛剛從上海華刪除,我只是不明白還有什麼能做些什麼來真的乾淨的東西了(雖然內存只有59M )

  • 唯一的那一刻,我有高記憶是,當我代替他們,而其僅爲1秒(134M!)

回答

1

如果你的應用程序是一個單一的視圖結構(沒有嵌入UINavigationControllerUITabBarController),你可以使用:

// App Delegate 
UIViewController *mainV=[self.storyboard instantiateViewControllerWithIdentifier:@"MatrixView"]; 
self.window.rootViewController = mainV; 
[self.window makeKeyAndVisible]; 
stripView = nil; 

舊視圖控制器將自動從內存中刪除,如果你使用ARC。

如果您在視圖控制器寫,你可以使用:

UIViewController *mainV=[self.storyboard instantiateViewControllerWithIdentifier:@"MatrixView"]; 
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
delegate.window.rootViewController = mainV; 
[delegate.window makeKeyAndVisible]; 
stripView = nil; 
+0

我不知道我明白你說的「可以使用」的意思是,我該怎麼辦,他的代碼?我必須能夠從B代替A,而不是從A代替B,我將如何做到這一點,以及我目前的代碼有什麼問題? – Curnelious

+0

更新後的代碼是否更清晰? – Raptor

+0

我不認爲與故事板,你需要使用setRootViewController或makrKeyAndVisible,因爲故事板爲你做這個。只是在appdelegate中定義的窗口並在applicationdidfinishlaunching中返回YES。 – Pawan

相關問題