2016-04-11 62 views
1

我需要保留子視圖控制器解除後將其移回需要時沒有額外的子視圖控制器處理。我曾嘗試使用下面的鏈接實現它:如何在移回父級後保留子視圖控制器(容器實現)?

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

How does View Controller Containment work in iOS 5?

這些鏈接(以及類似的其他人)都將成爲子視圖控制器或貶,但不是「保留它」的目的。請在下面找到我的代碼:

/* Adding a child view controller */ 
self.detailsViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailsViewController"]; 
self.detailsViewController.name = @"DetailsText"; 
// data to do "processing in viewDidLoad of child" 
self.detailsViewController.someOtherDataForProcessing = someOtherDataForProcessing; 
self.detailsViewController.delegate = self; 
[self addChildViewController:self.detailsViewController]; 
self.detailsViewController.view.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 200); 
[self.view addSubview:self.detailsViewController.view]; 


/* Bringing the child up on swipe gesture */ 
[UIView animateWithDuration:0.5 animations:^{ 
    self.detailsViewController.view.frame = CGRectMake(0, 100, self.view.frame.size.width, 200); 
} completion:^(BOOL finished) { 
    [self.detailsViewController didMoveToParentViewController:self]; 
}]; 


/* Moving child down when back pressed on child */ 
[UIView animateWithDuration:0.5 animations:^{ 
    [self.detailsViewController willMoveToParentViewController:nil]; 

    self.detailsViewController.view.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 200); 
} completion:^(BOOL finished) { 
    [self.detailsViewController.view removeFromSuperview]; 
    [self.detailsViewController removeFromParentViewController]; 
}]; 

,如果我需要「刷卡上再次父」把孩子控制,我不得不再次經歷的整個過程。我需要做的只是「讓孩子在滑動手勢上」過程,而不是再次實例化,因爲實例化將在子控制器中處理數據(耗時)。

我是iOS應用程序編程的noob,所以如果這是一個明顯的問題,請耐心等待。

+0

如何聲明'self.detailsViewController'?它應該是'強'。 – Cyrille

+0

@property(強,非原子)DetailsViewController * detailsViewController; 我認爲這個問題是在這幾行: [self.detailsViewController willMoveToParentViewController:nil]; [self.detailsViewController.view removeFromSuperview]; [self.detailsViewController removeFromParentViewController]; 但我不能刪除它們可以嗎? –

回答

0

好吧首先,你需要確保你的detailsViewController屬性聲明具有較強的參考,像這樣:

@property (strong, nonatomic) UIViewController *detailsViewController; 

這意味着只要聲明該屬性類是仍在內存,那麼這個屬性也會如此。 「強」意味着它即使在不再被使用的情況下仍然存在。

現在創建/刪除此對象。你在那裏顯示的三段代碼大致等同於:實例化/顯示/隱藏(和刪除)。

取而代之 - 只需在viewDidLoad之類的方法上實例化一次部件,以便只創建一次viewController。展示時,只需執行您的動畫代碼即可。隱藏時,不要從removeSuperview中刪除。所以它只是留在原地,在記憶中。它將存在,就在屏幕之外,準備好讓它再次將其動畫回憶。

還有什麼不清楚的,請大聲點。

+0

嗨盧克,你的意思是說這個隱藏代碼是正確的? [self.detailsViewController willMoveToParentViewController:nil]; - 不要刪除 [self.detailsViewController.view removeFromSuperview]; - 刪除 [self.detailsViewController removeFromParentViewController]; - 刪除 –

+0

不要:removeFromParentViewController,並且不要removeFromSuperview。調用willMoveTo等只是發送消息到子視圖控制器。想一想 - 你正在添加一次,並始終保存在內存中,永遠不要移除 - 只需將它從動畫中移出屏幕即可。移動動畫是動畫..方法的setFrame部分。 –

+0

謝謝盧克。這很好。 –

0

也許它有幫助。 在你的父類:

 if (self.arrayControllers == nil) { 
      self.arrayControllers = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers]; 
     } 
     for (UIViewController *vc in self.arrayControllers) { 
      if ([vc isKindOfClass:[YourRetainedViewController class]]) 
       controller = vc; 
     } 
     if (controller == nil) { 
      controller = [[YourRetainedViewController alloc] init]; 
      [self.arrayControllers addObject:controller]; 
     } 
     [self.navigationController pushViewController:controller animated:YES]; 

這裏:

self. self.arrayControllers - 數組中ParentViewController,是一個強大性能。 YourRetainedViewController - ChildViewController。

所以當彈出到父視圖控制器 - 這裏是一個數組,其中包含childViewController的一個實例。

+0

謝謝你的回答尼克。但盧克已經給了我一個滿意的:) –

相關問題