1

子View Controllerで追加の処理を行うことなく、必要に応じて子View Controllerを元に戻すために、子View Controllerを破棄した後も保持する必要があります。以下のリンクを使用してそれを達成しようとしました:

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

View Controller Containment は iOS 5 でどのように機能しますか?

これらのリンク(および同様の他のリンク)は、子View Controllerを持ってくるか、それを却下する目的を果たしましたが、「保持」することはありませんでした。以下の私のコードを見つけてください:

/* 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 アプリ プログラミングの初心者なので、これが明らかな質問である場合はご容赦ください。

4

2 に答える 2

0

まず、 detailsViewController プロパティが次のような強い参照で宣言されていることを確認する必要があります。

@property (strong, nonatomic) UIViewController *detailsViewController;

これは、このプロパティを宣言するクラスがまだメモリ内にある限り、このプロパティもメモリ内にあることを意味します。「強い」とは、もはや使用されていないと見なされても、それが残ることを意味します。

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

代わりに、viewDidLoad などのメソッドでインスタンス化の部分を 1 回実行するだけで、この viewController が 1 回だけ作成されます。表示するときは、アニメーション コードを実行するだけです。非表示にする場合は、removeFromSuperview を実行しないでください。そのため、メモリ内のその場所にとどまります。画面の外に存在し、アニメートして再びメモリに戻す準​​備ができています。

他に不明な点がありましたら、お声をかけてください。

于 2016-04-11T09:55:49.040 に答える