I don't know why dismissViewControllerAnimated:completion:. I just want to do it.
I start with a
[self performSegueWithIdentifier:#"my_segue" sender:self];
But is I call the dismiss than nothing happens. I can create another segue, but it create a new view controller.
My question is: How dismiss the performSegueWithIdentifier:sender:?
Do you have a navigationBar in the viewController that's calling:
[self performSegueWithIdentifier:#"my_segue" sender:self];
If so, you'll need to use:
[self.navigationController popViewControllerAnimated:YES];
to pop the view off the stack. There's one segue call, but the framework seems to call:
presentViewController:animated:completion:
or:
pushViewController:animated:
as appropriate.
Ray
You could just call
[self dismissViewControllerAnimated:YES completion:nil];
from the view controller since the view controller was pushed by segue.
[my_segue_view_controller dismissModalViewControllerAnimated: YES] ?
(not sure, but it works in my practice)
performSegueWithIdentifier:sender: itself isn't dismissed, that's just the method that's called to initiate a named segue. What happens in the segue is of more interest.
You're right that you should call dismissViewControllerAnimated:completion:, and it should be called by the presenting view controller, which has previously called the presented view controller using presentViewController:animated:completion:. For more info, see the UIViewcontroller docs.
Use the below code for Swift 4 version
self.navigationController?.popViewController(animated: true)
Related
I need to go to the first view in my app. I have a few views pushed onto the stack then a modal navigation controller and more views pushed onto that.
The problem I'm having is that using [[self navigationController] popToRootViewControllerAnimated:YES]; only goes back to the first view in the modal stack.
And I can't get [[self navigationController] popToViewController:.. to work because the true first view controller isn't accesible with [[self navigationController] viewControllers].
Any ideas on how to accomplish this? Thanks.
Do this:
[[self navigationController] dismissModalViewControllerAnimated:YES];
That will get you back to the VC that modally presented the navigation controller. Getting farther back after that depend on how you pushed those "few views" before the navigation controller.
Edit - explanation to get to the deepest root...
It sounds like those "few views" are on another, underlying navigation controller's stack. This can be a little tricky, because the clean way to get farther back in that stack is to have that underlying navigation controller pop to it's own root. But how can it know that the modal VC on top of it is done?
Let's call the view controller that did the modal presentation of second navigation controller VC_a. It's a modally presented navigation controller whose topmost VC is VC_b. How can VC_a know to pop to it's navigation root when VC_b modally dismisses itself?
The good answer (usually) is that VC_b decided to dismiss itself for a reason - some condition in your app/model changed to make it decide to be done.
We want VC_a to detect this condition, too. When VC_b gets dismissed, and VC_a gets a viewWillAppear message because it's about to be uncovered:
// VC_a.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (/* some app condition that's true when VC_b is done */) {
// I must be appearing because VC_b is done, and I'm being uncovered
// That means I'm done, too. So pop...
[self.navigationController popToRootViewControllerAnimated:NO];
} else {
// I must be appearing for the normal reason, because I was just pushed onto the stack
}
}
You need to do it by using the delegation pattern. Specifically, by creating a protocol that implements the delegate's respondsToSelector method.
See this post for complete details. It should be almost exactly what you are looking for. I had to do something similar, except I only needed to pop one view off the navigation stack instead of using popToRootViewControllerAnimated:.
For iOS6...
[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
In AppDelegate.m class create method with bellow flow...
-(void)MethodName{//your method name
YourViewController *objViewController = [[[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil] autorelease]; ///define your viewcontroller name like "FirstViewController"
UINavigationController *yourNavigationController = [[[UINavigationController alloc] initWithRootViewController:objViewController] autorelease];
self.window.rootViewController = yourNavigationController;
}
When you want redirect on firstview just call this method from appdelegate object....
-(IBAction) btnReturn:(id) sender{
firstView * firstview =[[firstView alloc]initWithNibName:#"firstView" bundle:nil];
[self.view pushViewController:firstview animated:NO];
}
with the previsly code I see the first view but the navigation control increment. I wold came bak as was the navigation starting point. Any help?
pushViewController:animated: will add to the navigation stack; you want popViewControllerAnimated: to go back one view in the stack.
If you want to return to the very first (root) view controller, you want popToRootViewControllerAnimated:.
See: https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html
In your above code you are pushing a controller on self.view (firstView, I am considering it as controller as you are creating it using initWithNibName: method, but you should you proper naming conventions to avoid confusions.) But view do not have any such method pushViewController:. Instead you sould use if you really have self (the controller in which you are using this IBAction) in navigation stack.
[self.navigationController pushViewController:firstview animated:NO];
To pop controller from navigation stack, follow what #gregheo suggested.
Try using popViewControllerAnimated instead:
- (IBAction)btnReturn:(id)sender
{
[self.navigationController popViewControllerAnimated:NO]; // Or Yes if you would like to have an animation.
}
Ok, i need help for this action in Table
You can either use -popToRootViewControllerAnimated: or -popToViewControllerAnimated:
In your case for returning to view 1, use:
[self.navigationController popToRootViewControllerAnimated:YES];
If you want to return to a specific view controller, get it's reference and then do:
[self.navigationController popToViewController: specificViewController animated:YES];
If you are using Storyboard...
Create a Segue pointing to View1 VC and perform that Segue when the Back button is triggered.
If not, Sid's answer is correct.
I have a view (viewB) that is pushed in using the navigation controller from another view (viewA) using pushViewController as usual, however for some reason, I want viewB's controller to pop the view using [self.navigationController popViewControllerAnimated:YES]; from inside its viewDidLoad method or viewDidAppear method, but none of them works, i.e. nothing happens (there is no crash in the app), however, i have a UIButton in viewB with IBAction that simply calls [self.navigationController popViewControllerAnimated:YES]; if the button tapped it will work and the view is popped off to the previous view !! this IBAction works if I removed [self.navigationController popViewControllerAnimated:YES]; from viewDidLoad or viewDidAppear methods because the popping will release the current view and all of its sub-views from memory.
the question is how can get the current view (viewB) to be popped off to the previous view (viewA) from inside viewDidLoad or viewDidAppear methods ?
thanks you so much in advance.
Try making method
- (void)popSelf {
[self.navigationController popViewControllerAnimated:YES];
}
In viewDidAppear add
[self performSelector:#selector(popSelf) withObject:nil afterDelay:0.0f];
This will add selector to runloop, so it will be performed after viewDidAppear.
I've got a view called A open with presentModalViewController Method, inside this view I loaded secondary view using:
new_view = [[new_websongs alloc] initWithNibName:#"new_websongs" bundle:nil];
[mysubview addSubview:new_view.view];
ok, to here it's ok but now I need to dismiss the first view "A" calling a method [self dismissModalViewControllerAnimated:YES] situated if first "A" viewController from secondary view controller (new_view) but not work! the code is:
self.Aviewcontroller = [[Aview alloc] init];
[Aviewcontroller dismissModalViewControllerAnimated:YES];
[Aviewcontroller release];
Please help ME!!!!
Thanks
Did u try [self.parentViewController dismissModalViewControllerAnimated:YES];
You have a logical problem. Start reading View Controller Programming Guide for iOS
The view controller that present an modal view controller must dismiss it or the modal view controller must dismiss it self
Totally agree with other answers; think logically about the order of view controller order and type. So think about which controllers are shown modally, and those shown via a navigation controller.
You can of course set a number of view controllers with:
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
without animation, then when required call say:
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
to show a specified view controller further up your stack of view controllers.
Hope this helps think about what you need to do? It's often a good idea to think about the order and type of view controllers in your app's interface in a separate project - where you can try it out on the device itself.
try this it should work
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
This works if you are presenting a modal view from a UISplitViewController. It can also be applied in so many other ways...
First, create an instance in your .h file for your appDelegate, (AppDelegate_iPad *appDelegate) then put this in your viewDidLoad or comparable method:
ipadDelegate = (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
Now, present the first modal view like this:
YOURVC *vc = [[YOURVC alloc] initWithNibName:#"YOURVC" bundle:nil];
[ipadDelegate.splitViewController presentModalViewController:vc animated:YES];
[vc release];
Say you have a subview, like a UITableView, and want to dismiss the modal from the didSelectRowAtIndexPath. All you have to do to dismiss your modal with a subview is create another ipadDelegate instance inside your subview's .h (if needed), reference the [[UIApplication sharedApplication] delegate] again, and dismiss:
[ipadAppDelegate.splitViewController dismissModalViewControllerAnimated:YES];
Essentially, as long-winded as it may be, use your appDelegate's controller to present and dismiss the the modal if you need to maintain a persistent reference to the presentingViewController...because all the things above just don't work in my case.
If you're presenting with your ipadDelegate, make sure you check the mode of presentation in your MainWindow_iPad.xib. Your "Transition Style" should be "Cover Vertical" and "Presentation" should be "Current Context" or your modal may present behind other views.