Cannot close two UiViewControllers - ios

I have a UiViewController A which opens a second UIViewController B as follows:
BViewController *BController = [Utilities getMainStoryBoardWithIdentifier:#"BViewController"];
[self presentViewController:webViewController animated:YES completion:nil];
Now on UiViewController B, when I want to close it, I would like to close the A also. So I'm using the following:
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
But only the B is closing...
Any idea ?

Can you please try following code :
[self dismissViewControllerAnimated:YES completion:nil];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
Where self is your B controller. I've just tried the code and it works well. Tell me if any other help is needed.

Try using an unwind segue.
If you have X which presents A, which in turn presents B, add
func unwindToX(_ segue: UIStoryboardSegue) { }
to view controller X
Then in your storyboard, drag from the close button in B to it's exit and select unwindToX. Clicking the button will then dismiss both view controllers. This also works with view controllers pushed on to a nav stack, or a combination of both.

Related

IOS Container View VC dismiss parent?

I have 3 view controllers. Controller A calls B. B has a container view which contains C - a table view controller. How do I use the didSelectRowAtIndexPath method in C to dismiss both B and C and return to A?
I've tried this:
[self.navigationController.parentViewController dismissViewControllerAnimated:YES completion:nil];
this:
[self.navigationController.parentViewController dismissViewControllerAnimated:YES completion:nil];
And this:
[self dismissViewControllerAnimated:YES completion:nil];
None of which work.

Modal View Controller, dismiss and pop back to view controller

Im trying to dismiss a ModaViewController called C, back to A. The ModalViewController is presented in B. Therefore the Navigation flow is A->B - (present ModalView) -> C. I am able to dismiss the ModalViewController back to B, but I am unable to pop back to A in the completion bracket. This is the code I have tried:
[self dismissViewControllerAnimated:YES completion:^{
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES];
}];
The ModalViewController is dismissed but does not pop back to A. I call this block of code in an IBAction.
Any advice?
On a second note, when I dismiss the ModalViewController all my UIPickers in Controller B are empty/ deallocated. I am using ARC as well.
The problem with your code is that self.navigationController will be nil. If you have a controller (A) embedded in a navigation controller, and that controller pushes to another controller (B) which then presents your last controller (C), then you need to do something like this,
-(IBAction)dismissToBThenPop:(id)sender {
UINavigationController *nav = (UINavigationController *)self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
[nav popViewControllerAnimated:YES];
}];
}
Even though you present C from B, the actual presentingViewController will be the navigation controller. This code will dismiss C then pop B, but you will see B for an instant before ut pops back to A. If you don't want to see this, then you should use an unwind segue to go directly back to A from C.
Your second problem about the pickers being empty and deallocated should not be happening under the scenario that you say you have. You will have to provide more information about what you're doing in B to solve that problem.
Create a protocol in ModalViewController, let's say ModalViewControllerDelegate with a method -(void)dismissTheModal, and make B implement this protocol. Before showing the ModalViewController, do modalViewController.delegate = self. When you're IBAction is called, do [self.delegate dismissTheModal], and in controller B you should do :
-(void)dismissTheModal {
[self dismissViewControllerAnimated:YES completion:^{
[self popViewController];
}];

Changing viewControllers with ECSlidingView

I have a view that contains a ECSlidingView:
Bij clicking the '+' button, you enter another view, without the ECSlidingView:
Now, I would like to return to the first view once a user clicks on the 'Voeg Toe' button in the second view. I have tried using:
FoodViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:#"Voedingsdagboek"];
[self presentViewController:myController animated:YES completion:nil];
However, now the ECSlider does not work anymore. So I cannot navigate through the rest of the application. How can I solve this?
To go back to previous view you can write popViewController in buttonTapped Method.
[self.navigationController popViewControllerAnimated:YES];
Eventually I fixed it by using:
self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Voedingsdagboek"];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

dismiss viewController by using presentingViewController

I have three ViewControllers and its order is (A present B which presents C),
when I stay in the C viewController ,I have to dismiss ViewController to B viewController.
for C viewController ,its presentingViewCOntroller is B viewController
of course ,I can use
[self dismissViewControllerAnimated:YES completion:NULL];//self means C ViewController
But I was wondering I can also use the following method:
[self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
because the presentingViewController of C is B ViewController, but it did the same effect.
self means C ViewController while self.presentingViewController means B ViewController,but they also did the same work
The second question is I cannot use the following to dismiss two viewController in succession:
[self dismissViewControllerAnimated:YES completion:^{
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}]; //self means C viewController
Thanks for your help!
of course ,I can use
[self dismissViewControllerAnimated:YES completion:NULL];//self means C ViewController
This only dismisses C, not B as you seem to want.
But I was wondering I can also use the following method:
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
Yes, this works. When in doubt, try it out.
The second question is I cannot use the following to dismiss two viewController in succession:
[self dismissViewControllerAnimated:YES completion:^{
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}]; //self means C viewController
That's not a question. Anyway, the reason it doesn't work is that after you dismiss yourself, your presentingViewController is nil. You need to store it in a temporary variable.
UIViewController *gp = self.presentingViewController.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
[gp dismissViewControllerAnimated:YES completion:nil];
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}];
Of course, the two will have different animations, you'll need to decide which you prefer.
see documentation:
The presenting view controller is responsible for dismissing the view
controller it presented. If you call this method on the presented view
controller itself, it automatically forwards the message to the
presenting view controller.
Please check: self.presentingViewController.presentingViewController is A viewController or not.
if not, I think you need use delegate or handler.
In my case, I used a tabbarcontroller as a rootViewController. When I used self.presentingViewController.presentingViewController, I got rootViewController (tabbarcontroller) which it have no response to presentingViewControler method.

Dissmis a modal viewcontroller on a Storyboard

I'm developing an iPhone application with latest SDK.
I don't know how to dismiss a ViewController. I've added a back button and this is what I do:
- (IBAction)backButtonClicked:(id)sender
{
[self.navigationController dismissModalViewControllerAnimated:YES];
}
I have also tried:
[self.navigationController popViewControllerAnimated:YES];
But either works. I've added a breakpoint and it stops on it.
And I use a Modal segue to navigate to this ViewController.
How can I dismiss this View Controller programmatically?
Try [self dismissViewControllerAnimated:YES completion:nil];
- (IBAction)backButtonClicked:(id)sender
{
[self.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
}
Is the VC being modally displayed an instance of your own view controller subclass, or did you wrap that inside a UINavigationController? If the former, try
[self dismissModalViewControllerAnimated:YES];
[self.presentingViewController dismiss...] will also work, but only for iOS 5+
create a custom segue,then button action choose the custom segue
http://jeffreysambells.com/2014/02/19/dismissing-a-modal-view-using-a-storyboard-segue

Resources