Dismissing 2 ViewController consecutively - ios

I try 2 ways to dismissed 2 viewcontrollers consecutively but only one of them got dismissed not the second one
method1
-(void) LoginDone:(NSNotification *)notif
{
[self dismissViewControllerAnimated:YES completion:NULL]; //previous viewcontroller
[self dismissViewControllerAnimated:YES completion:NULL]; //current viewcontroller
}
method2
-(void) LoginDone:(NSNotification *)notif
{
[self dismissViewControllerAnimated:YES completion:NULL];
[[NSNotificationCenter defaultCenter] postNotificationName:#"LoginDone2" object:nil];
}
-(void) LoginDone2:(NSNotification *)notif
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
I need to find out a way to dismiss both the previous viewcontroller and current viewcontroller consecutively.

This is now an old question, but it seems to be exactly the problem I am having presently.
Here what I did:
[self.presentingViewController.presentingViewController
dismissViewControllerAnimated:YES completion:nil];
And it works for me. I hope it can be useful to someone.

By calling
[self dismissViewControllerAnimated:YES completion:NULL];
you are telling self to dismiss the view it presented. Telling twice the same self object to dismiss the view it presented, will not change the result. In other words self cannot represent the "current view" and the "previous view" at the same time as per your comment to the code. self is just a single controller representing a single view, either the current or the previous one.
To fix this, you should send the dismissViewControllerAnimated to self (that presented the top-most view, I assume) and to the other view controller object that presented the previous view.
In other words, I would expect something like this:
-(void) LoginDone:(NSNotification *)notif
{
[self dismissViewControllerAnimated:YES completion:NULL];
[self.previousController dismissViewControllerAnimated:YES completion:NULL];
}
Actually, you could send just one message to the second view controller and both views would be dismissed (source):
If you present several view controllers in succession, and thus build a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

I know this is an old question but maybe somebody will look for solution on this issue so here it is:
-(void) closeModalViews
{
[previousVC dismissViewControllerAnimated:YES completion:^(void) {
[self dismissViewControllerAnimated:YES];
}];
}

I like JPetric's idea, but first you must dismiss the current view controller's view and only then can you dismiss the presenting view controller's view.
[self dismissViewControllerAnimated:NO completion:^(void) {
[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
}];

As far as I could understand you are trying something like below:
There are 2 view controllers.
You want both of them to be vanished.
Another controller comes in.
But the fact is only one controller is displayed, why you would need to dismiss 2 then?

You can use self.view.hidden=true; on the jumped viewcontrollers while animating back to the first viewcontroller. Using [self.presentingViewController dismiss...] is not working for me without hiding.

Related

Dismissing previously presented modal view controller before currently visible one

I have 2 UIViewController's presented with [self presentViewController:viewController animated:YES completion:nil];, I want to dismiss the first one of them, without animation (It's not visible to the user anyway) and when the second one (currently visible) will be dismissed, the user will see the parent view controller who present them both.
- Parent
- First -> Dismiss first without animation
- Second -> Dismiss second with animation
How can I do that?
With your current view controller hierarchy if first view controller will be dismissed it will dismiss second view controller automatically. If you don't want that behaviour than make parent present second view controller. You can do that from first view controller by using [self.presentingViewController presentViewController:secondViewController animated:YES completion:nil]
Why do you want to do this?
You should do it like this for cleaner view hierarchy and better user experience:
Present first view controller :
[self presentViewController:viewController1 animated:YES completion:nil];
Dismiss first & present second view controller :
__weak MyViewController *aBlockSelf = self;
[self dismissViewControllerAnimated:YES completion:^{
[aBlockSelf presentViewController:viewController2 animated:YES completion:nil];
}];

Dismiss and Present same view controller

i have a view controller and i need to dismiss it and present it back in same time.
i had tried dismiss it and call back the view controller but not working.
[self dismissViewControllerAnimated:YES completion:nil];
UIStoryboard *storyboard=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
ExpandViewController *expandView=
[storyboard instantiateViewControllerWithIdentifier:#"ExpandViewController"];
expandView.delegate=self;
[expandView setEventDict:dict];
[self presentViewController:expandView animated:YES completion:NULL];
I am not exactly sure what outcome/functionality you are looking for in your question, but #matt is correct. However, you may be looking to have this happen seamlessly. Therefore you could use child view controllers instead of presenting the view controller using the [self presentViewController:VC animated:animate completion:nil] method.
Adding child vc:
[self addChildViewController:myVC];
[self.view addSubview:myVC.view];
[myVC didMoveToParentViewController:self];
Removing child vc:
[myVC willMoveToParentViewController:nil];
... remove subview.
You can set up a delegate between the two controllers to tell the parent when to dismiss the view to make things easy. You can also add the subviews at different indexes using [self.view insertSubview:myVC atIndex:index] or the other possible functions such as the insert above subview etc, to have one subview be added before dismissing the other to give a more seamless transition.
Hope this helps!
You can't present a view controller until the currently presented view controller has finished being dismissed. You won't know this has happened until the completion handler from your dismissal is called. Your mistake is that the completion handler is nil. Instead, provide a completion handler (in your first line), consisting of the remaining lines of your code. Thus, they will execute after the dismissal finishes.
[self dismissViewControllerAnimated:YES completion:^{
// ... the rest of your code goes in here ...
}];

dismissViewController clear the screen but doesn't dismiss

I have 2 screens:
The first call the second with the following code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:kDetailSegue]) {
[NSThread detachNewThreadSelector:#selector(loadingThread) toTarget:self withObject:nil];
TagDetailControllerViewController* detailController = segue.destinationViewController;
detailController.location = self.recentLocation;
}
}
The kDetailSegue is a present modally - presentation default - transition: default
In second ViewController, I'm trying to dismiss the screen:
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
It is not clear what you want to dismiss in your second view controller, so I will try to present the most likely scenarios:
If you want to dismiss the view controller presented modally (TagDetailControllerViewController), you need to dismiss it modally too.
This can be achieved by calling in your TagDetailControllerViewController itself:
[self dismissViewControllerAnimated:YES completion:nil];
Which works because, according to Apple's documentation:
If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.
Also, if you want to dismiss it directly from the view controller you used to present it, you can do it as follows:
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
Finally, if what you want is to dismiss a view that was pushed in your UINavigationController, then you can just pop it from the stack:
[self.navigationController popViewControllerAnimated:YES];
As you can see, the way a view is dismissed really depends on the way it was presented.
try this
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];

Switching two view controller, back function not working

im have two view controller UserListView and UserProfileView!
in UserListView view controller i'm have a button for swtich to UserProfileView and here is code.
UserListView.m - Click Action
- (IBAction)SettingClick:(id)sender
{
UserList *UserProfile = [self.storyboard instantiateViewControllerWithIdentifier:#"UserProfileView"];
[self presentViewController:UserProfile animated:YES completion:nil];
}
And code working fine, when user switch to profile (UserProfileView) have a close button back to UserListView and here is code.
UserProfileView.m - Close click action
- (IBAction)CloseClick:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
UserProfile *UseList = [self.storyboard instantiateViewControllerWithIdentifier:#"UserListView"];
[self presentViewController:UseList animated:YES completion:nil];
}
in this code i will using [self dismissViewControllerAnimated:YES completion:nil]; to close UserProfileView view controller for low ram usage and it work.
But affter i close UserProfileView i want to open this view controller again and it do not work, UserProfileView not showing again??
i using xcode 5 and building an App for ios 7, please help.
Thanks for your time.
If I understand correctly, when you call SettingClick: your app is displaying a UserList. So, when you dismiss a view controller presented on top of it, you should go back to UserList without the need for presenting it again. So you can try with:
- (IBAction)CloseClick:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
This will work unless you had originally presented UserList as well. In this case, UserList will be dismissed with the top controller. In this case, you can delay presenting a second time UserList after dismissing UserProfile, and it should work.
In the latter case, I would suggest you to use a navigation controller instead of simply presenting your controllers like you are doing. As you see, it is not really straightforward and you will get into catches of any kind. Presenting a controller works ok when you present just one controller at a time. On the other hand, if you instantiate a UINavigationController, this will handle the controllers' hierarchy for you.
use this -
- (IBAction)CloseClick:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
This will return to your previous view when u click that button that has been linked with this action

IOS: dismiss two viewController

I have three viewController
First, Second and Third
from Second to open Third I use
Third *third = [[Third alloc]initWithNibName:#"Third" bundle:nil];
[self presentModalViewController:third animated:YES];
[third release];
Now I want return from third to first; then I set in viewDidAppear in second this code:
[self dismissModalViewControllerAnimated:NO];
but for 1 second I see Second and I don't want watch it...how can I do?
You need to dismiss third view controller first and then second Viewcontroller. Do the following code when you want to go first view controller.
-(void)goToFirstView{
UIViewController *vc = [self parentViewController];
// UIViewController *vc = [self presentingViewController]; //ios 5 or later
[self dismissModalViewControllerAnimated:NO];
[vc dismissModalViewControllerAnimated:YES];
}
How is the Third modal view being dismissed in the first place? Perhaps by the user touching a 'Done' button? If so, it is in the handler for the button that you want to dismiss both.
You can dismiss both as:
[self dismissModalViewControllerAnimated: YES];
[self.presentingViewController dismissModalViewControllerAnimated: NO];
This happens coz viewDidAppear is called everytime before the view appears so as soon as it appears you dismiss it and it disappears..
I don't think what u are trying to do can be achieved with modalViewControllers...
instead use a navigationController and keep adding your viewcontrollers onto the stack and when you want to goto the First view controller just call
[self.navigationController popToRootViewControllerAnimated:YES];
EDIT:
just thought of it this can be achieved by using delegation.. you make second the delegate of third and as soon you dismiss the thirdviecontroller send the delegate a message.In this message call [self dismissModalViewControllerAnimated:NO];..
and you are done.. (pretty easy if you know delegation.)

Resources