2016-04-11 18 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]; 
}]; 

i「は、親のスワイプで再度」子コントローラを持参する必要がある場合、私は再び全体のプロセスを通過する必要があります。私が必要とするのは、「スワイプジェスチャーで子を持ち上げる」プロセスを実行するだけで、インスタンス化は子コントローラのデータ処理(時間のかかる)を行うため、再度インスタンス化しないことです。

私は、iOSアプリケーションプログラミングのnoobですので、これが明白な質問であれば私にご負担ください。

+0

'self.detailsViewController'はどのように宣言されていますか?それは '強い'であるべきです。 – Cyrille

+0

@property(強い、非原子的)DetailsViewController * detailsViewController; 問題は次の行にあると思います。 [self.detailsViewController willMoveToParentViewController:nil]; [self.detailsViewController.view removeFromSuperview]; [self.detailsViewController removeFromParentViewController]; でも削除できませんか? –

答えて

0

[OK]を、最初に、あなたdetailsViewControllerプロパティが強い参照して宣言されていることを確認する必要があり、このような何か:

@property (strong, nonatomic) UIViewController *detailsViewController; 

これはその限り、このプロパティを宣言するクラスがメモリに残っているよう意味それでは、このプロパティもそうです。 「強い」とは、もはや使用されていなくても、それがとどまることを意味する。

このオブジェクトの作成/削除についてです。ここで示したコードの3つのセクションは、インスタンス化/表示/非表示(および削除)とほぼ同じです。

代わりに、インスタンス化部分をviewDidLoadのようなメソッドで1回実行するだけで、このviewControllerが作成されます。表示するときは、アニメーションコードを実行してください。隠れているときは、removeFromSuperviewをしないでください。だから、それはどこにあるのか、記憶に残っているだけです。それは画面のすぐ外に存在し、再びアニメーション化されてメモリに戻ってきます。

それ以外は明確ではありません。叫んでください。

+0

こんにちはルーク、あなたは正しいコードを隠すためにこれを言っているのですか? [self.detailsViewController willMoveToParentViewController:nil]; - 削除しないでください [self.detailsViewController.view removeFromSuperview]; - remove [self.detailsViewController removeFromParentViewController]; - –

+0

を削除してください:removeFromParentViewController、およびremoveFromSuperviewを削除しないでください。 willMoveToなどの呼び出しは、子ビューコントローラにメッセージを送信するだけです。あなたはそれを一度加えて、常にメモリに残しておきます。決して取り除くことはありません。アニメーションを使ってスクリーンから外すだけです。 move animはアニメーションの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

回答ありがとうNik。しかし、ルークはすでに私に満足のいくものを与えてくれました:) –

関連する問題