3

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!

Shekhar Gupta
  • 6,056
  • 3
  • 27
  • 49
user2334616
  • 31
  • 1
  • 1
  • 3
  • Not sure if that's the problem here, but you shouldn't have to call `didMoveToParentViewController:` with a `nil` parameter, `removeFromParentViewController` should call that automatically after the view controller was removed. – omz Apr 30 '13 at 06:12
  • even if I remove [self didMoveToParentViewController:nil];, it still pauses the application and later on crashes. – user2334616 Apr 30 '13 at 06:16

7 Answers7

3

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.

RegisteredUser
  • 415
  • 4
  • 13
2

You should use delegation. When you click the button in your ReviewViewController, you call a method like: [self.delegate hideReviewController:self];

And this method would look something like:

- (void)hideReviewController:(ReviewViewController *)controller {
                [UIView animateWithDuration:0.3
                                  delay:0
                                options:UIViewAnimationOptionCurveEaseInOut
                             animations:^{
                                  controller.view.alpha = 0;
                             } completion:^(BOOL finished) {
                                  [self.view removeSubview:controller.view];
                                  // If you want the Review Controller to be deallocated:
                                  [self removeChildViewController:controller];
                             }];
}

EDIT: In your ReviewDelegate.h file add:

@class ReviewViewController;

@protocol ReviewDelegate <NSObject>
- (void)hideReviewController:(ReviewViewController *)controller;
@end

Then add this property:

@property (nonatomic, weak) id <ReviewDelegate> delegate;

In the parent class:

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

    - (void)hideReviewController:(ReviewViewController *)controller {
                    [UIView animateWithDuration:0.3
                                      delay:0
                                    options:UIViewAnimationOptionCurveEaseInOut
                                 animations:^{
                                      controller.view.alpha = 0;
                                 } completion:^(BOOL finished) {
                                      [self.view removeSubview:controller.view];
                                      // If you want the Review Controller to be deallocated:
                                      [self removeChildViewController:controller];
                                 }];
    }

I also suggest that you read about delegation

Levi
  • 7,062
  • 2
  • 28
  • 39
2

Try this

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

You don't need to make it complex... Just use UINavigationController.

To navigate from ParentVC to ChildVC :

  ChildViewController *ChildVC = [[ChildViewController alloc]initWithNibName:@"ChildViewController" bundle:nil];
[self.navigationController pushViewController:ChildVC animated:YES];

And for navigating back from ChildVC to ParentVC :

ParentViewController *ParentVC = [[ParentViewController alloc]initWithNibName:@"ParentViewController" bundle:nil];

[self.navigationController popViewControllerAnimated:YES];
Vineet Singh
  • 4,775
  • 1
  • 28
  • 39
1

Since you are adding the Child View as a Subview on to Parent you need to manually it from its superview (in your case parentview). Rather then adding it as a subview you can make use of presentViewController method as follow:

 - (IBAction)btnReview:(id)sender
  {

   ReviewViewController *vc = [[ReviewViewController alloc] initWithNibName:@"ReviewViewController" bundle:nil];
   [self presentViewController:vc
                      animated:YES
                    completion:nil];
  } 

and in the child class code for back button will be :

  -(IBAction)backButtonClicked:(id)sender
  {
      [self dismissViewControllerAnimated:YES completion:nil];
  }
Gaurav Rastogi
  • 2,105
  • 1
  • 12
  • 19
0

Sounds like what you're looking for could be taken care of with a UINavigationController. Just use pushViewController: to push the new view onto the screen. Then, a back button will appear in the navigation bar atop the view, which can be used to return to the "parent" view controller.

See the documentation.

The Kraken
  • 3,087
  • 5
  • 25
  • 65
0

writing only

- (IBAction)backbutton:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

worked in my case

Ingo Karkat
  • 154,018
  • 15
  • 205
  • 275
ghoshghosh
  • 166
  • 1
  • 1
  • 9