Reputation: 10412
I have a view controller. I have a flag called: "dismissOnAppear". basically if this flag is set to YES, I want to Dismiss my view controller as soon as the app returns to it.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(self.dismissOnApear)
{
[self dismissViewControllerAnimated:NO completion:nil];
}
}
The problem is that the view is appearing for a split second and then dismissing. Is there a way I can dismiss the view controller when I return to it, without it displaying for a split second?
Edit: For more context:
I have ViewControllers A, B And C. A present B, then B present C. I want to return to A when I close C, and not to B. The views are presented modally, doing it through navigation controller and push is not an option now, so is there a way to achieve that?
Thanks
Upvotes: 0
Views: 191
Reputation: 36
Just loop through the presenting controllers until you reach the parent, and dismiss the view from there.
UIViewController *vc = self.presentingViewController;
while (vc.presentingViewController) {
vc = vc.presentingViewController;
}
[vc dismissViewControllerAnimated:YES completion:NULL];
Upvotes: 1
Reputation: 3145
Here's seamless dismissing animation for structure A>B>C
with two modal view controllers.
In order to hide B
controller when dismissing both B
and C
controllers, make a snapshot view of C
controller when presenting it (in B
controller) and display it:
[self presentViewController:cViewController animated:YES completion:^{
UIView *snapshotView = [cViewController.view snapshotViewAfterScreenUpdates:YES];
[self.view addSubview:snapshotView];
}];
Then, when you need to dismiss both controllers (in C
controller) just call this method on C
controller:
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
It will display B
controller shortly, but don't worry we already hide it under C
controller snapshot :)
Upvotes: 0
Reputation: 4111
1.Make a public property in CViewController as:
@property(nonatomic, weak) BViewController *bViewController;//please import BViewController//must be **weak**(avoid retain cycle)
2.When you show CViewController from BViewController
CViewControllerObj.bViewController = self;
3.Now when you are in CViewController cal this:
- (void)turnBackToAViewController{
[self dismissViewControllerAnimated:YES completion:^{
[self.bViewController dismissViewControllerAnimated:NO completion:^{
}];
}];
}
Hope it will help :)
Upvotes: 0
Reputation: 4111
you can do like this. In C view controller class call this method when you want (example button click or whatever ever):
- (void)turnBackToAViewController{
for (UIViewController *controller in self.navigationController.viewControllers) {
//Do not forget to import AViewController.h
if ([controller isKindOfClass:[AViewController class]]) {
[self.navigationController popToViewController:controller
animated:YES];
break;
}
}
}
Upvotes: 2
Reputation: 3763
Can you maybe just keep a weak reference in C to ViewController A and tell VC A to dismiss the currently presented viewController (which is B, so it might take C along with it).
Upvotes: 0
Reputation: 3763
The easies way would be to use a UINAvigationController, but that seems not an option. If the reason to avoid the navigationController is because you want your won animations, you can still use navigationController, and use custom transitions (see the UINavigationControllerDelegate method:
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0);
Another way is to implement your own containerViewController with a similar interface as a UINavigatioController, which handles exactly what you want (as you write the code for it). In order to achieve this, you need to know about parent and childviewController, and checkout Apple's containerViewController guide:
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html
Can you explain why it is you do now want to use a UINavigationController?
Upvotes: 0