Shift to Parent View Controller from child view controller - iOS

user2334616 picture user2334616 · Apr 30, 2013 · Viewed 11.1k times · Source

I have a main Actions View Controller, and on that there is button "Review". Review Button's functionality is :

 - (IBAction)btnReview:(id)sender
{
    ReviewViewController *vc = [[ReviewViewController alloc]initWithNibName:@"ReviewViewController" bundle:nil];
    [self addChildViewController:vc];
    [vc didMoveToParentViewController:self];
    [self.view addSubview:vc.view];
}

Now on Review Page, I have a button. And on that button's action, I want to switch back to Parent View Controller. Its view should be displayed. I have tried following code, it either pauses or crashes application.

[self didMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];

I even tried:

[self dismissModalViewControllerAnimated:YES]

I have read other posts related to this question as well but could not find a satisfactory answer. Please Help!

Answer

RegisteredUser picture RegisteredUser · Aug 18, 2013

I'm assuming that this is a custom viewcontroller container you're trying to build, "self" is the Main Actions viewController you're talking about.

Try this:

- (IBAction)btnReview:(id)sender
{
    ReviewViewController *vc = [[ReviewViewController alloc]initWithNibName:@"ReviewViewController" bundle:nil];

    [self addChildViewController:vc];

    [self.view addSubview:vc.view];

    [vc didMoveToParentViewController:self];

    vc = nil;

}

For the "Back" button, I'm assuming this is the Button to return to Main Actions viewController from the Review viewController, try this:

- (IBAction)btnReviewHide:(id)sender
 {

    [self willMoveToParentViewController:nil];
    [self.view removeFromSuperview];
    [self removeFromParentViewController];

 }

Hope this helps.