Shift to Parent View Controller from child view controller - iOS
Solution 1
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.
Solution 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
Solution 3
Try this
self.view=nil;
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
Solution 4
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];
}
Solution 5
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];
user2334616
Updated on June 06, 2022Comments
-
user2334616 almost 2 years
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!
-
user2334616 almost 11 yearsNo, I am not using UINavigationController, as my application is Tab based.
-
The Kraken almost 11 yearsSee Levi's answer above, then.
-
user2334616 almost 11 yearsNavigating from ParentVC to ChildVC is working fine, but crashes when navigating back from ChildVC to ParentVC.
-
Vineet Singh almost 11 yearsHave you implemented 'pushViewController:' for navigation from ParentVC to ChildVC????
-
Vineet Singh almost 11 yearsHave you implemented
UINavigationControllerDelegate
in your interface file of your parentVC as well as in ChildVC.