I have a View that has a button in it, something like this Button---(segue)---> TableViewControllerNO1-----(segue)-->TableViewControllerNO2. In the TableViewControllerNO2 I delete all the rows by pressing a button and I want after I delete the rows to return to the first View, the one before the TableViewControllerNO1, how can I dismiss the two TableViewControllers from TableViewControllerNO2?
You have a cool method to do this: popToViewController
[self.navigationController popToViewController:viewController animated:YES];
So the important is inject a reference to that viewController.
Otherwise you can also do:
[self.navigationController popViewControllerAnimated:NO];
[self.navigationController popViewControllerAnimated:YES];
but the right way is the first.
You should use - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated (assuming you are using a navigation controller). This would take you all the way back to the root view controller in your navigation stack, and use the standard animation.
Related
So I was create three UITableViewControllers with UINavigationController. I want a back button on 3rd UITableViewController, what returns my view to first UITableViewController instead of second.
How can I do that? That must be a real backButton, not a image or something else. Will be perfect to do this only with storyboard.
UPDATE
Perhaps I poorly explained what I want.
I don't want use any button with action on it. I just want something like as setting "address" of 1st TableViewController on my default back button. There is any way to do it?
add a button and connect it to following action
- (IBAction)backToFirstView:(UIButton *)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
(or)
[self.navigationController popToViewController:yourFirstViewControllerObject animated:YES];
}
There are different ways to navigate from DetailViewController to other view controllers.
We will go through the cases one by one.
First of all I would like to clear that if its your default
navigation bar's back button, then it must return to the last most
view controller only which is actually a default behavior of a
navigation controller.
Second, If you would like to go back to the
last most view controller on the tap of a button placed by you, you
should write the following code
[self.navigationController popToViewController:NAME_OF_A_VIEWCONTROLLER animated:YES];
Third, If you would like to go to the first view controller from where you
started, you should write the following code
[self.navigationController popToRootViewControllerAnimated:YES];
Ok, I found a way to resolve my problem. Thanks for your answers guys, they was very helpful.
So for resolve this problem you just need use link what give me Kumar KL upper, and wrote next method in your UITableVIewController
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// Navigation button was pressed. Do some stuff
[self.navigationController popViewControllerAnimated:NO];
}
[super viewWillDisappear:animated];
}
Now you got a backButton what redirect you to your viewController, BUT title of this button is wrong. Let's resolve that unfair.
Create new class CustomSegueцрфе inherited from UIStoryboardSegue with next code in CustomSegue.m :
- (void)perform
{
UIViewController *sourceView = (UIViewController *) self.sourceViewController;
UIViewController *destinationView = (UIViewController *) self.destinationViewController;
[[destinationView navigationItem] setTitle:#"TitleOfYourViewController" ] ;
[sourceView.navigationItem setTitle:#"TitleOfButton"] ;
[sourceView.navigationController pushViewController:destinationView animated:YES];
}
Now you can go to storyboard and connect 2nd ViewController with 3rd with custom segue.
Like you see UINavigationController uses Title of previous ViewController for button title, so you just need change it.
I am experiencing an issue and I believe it's due to me incorrectly removing my view. I will try to explain and hope all can follow.
I have a UISplitview app which essentially has two views. The main detail view, and a view which is essentially a map. Upon selecting a certain string in the table this code is called (this code is in the detailview but the rainObject is passed in from a UIViewController.
-(void)setReceivedRainObject:(id)receivedRainObject{
if ([receivedRainObject isEqualToString:#"Test"]){
mapViewController *mapView=[[mapViewController alloc]initWithNibName:#"mapViewController" bundle:nil location:0];
[self.navigationItem setHidesBackButton:NO animated:NO];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self.navigationController pushViewController:mapView animated:NO];
}
}
So the above code loads my map fine in the detailscreen as expected.
However, when I click the first option (test) from the left view controller, it calls a new view controller (viewcontroller2 with a row named test2) and that second controller calls a third( viewcontroller3 with a row named test3)
On the viewDidDisappear of viewcontoller3 and in the viewWillAppear of test I have this code
[self.detailViewController.navigationController popViewControllerAnimated:NO];
So when I drill down to viewcontroller, when I click the backbutton, it pops off one map, and when testview loads it pops the second. leaving my detail view showing exactly how I started.
However, I've noticed that if I clicked the "test" button twice, and try to go back, there is an "extra screen" that was supposed to be remove with pop. Would like to know how to remove this mass of views or to stop the program from adding so many.
Thanks
Just use
mapViewController *mapView=[[mapViewController alloc]initWithNibName:#"mapViewController" bundle:nil location:0];
[self.navigationController pushViewController:mapView animated:YES];
to navigate through the view controllers.
In my app, I want to remove a view controller from the stack of view controllers and then navigate to a particular view controller. Say I have A,B,C,D,E view controller stack. I wish to remove E and then go to C. I use the following code
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
for (UIViewController *aViewController in allViewControllers)
{
if ([aViewController isKindOfClass:[noNetworkViewController class]])
{
[aViewController removeFromParentViewController];
}
}
NSMutableArray *allbViewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
for (UIViewController *aViewController in allbViewControllers)
{
if ([aViewController isKindOfClass:[messageViewController class]])
{
[self.navigationController popToViewController:aViewController animated:NO];
}
}
The first for loop erases all the view controllers from the stack. Is there something I am doing wrong here. Please let me know.
You should not be mucking around with a navigation controller's stack of view controllers using removeFromParentViewController:. Ever. That is flat-out wrong.
Furthermore, there is no reason to loop through a navigation controller's stack of view controllers and popping them one by one. There are methods like popToViewController:animated: and popToRootViewController that let you pop to any arbitrary view controller in the stack with one call.
Have you verified that the parent view controller property is populated by the navigation controller? The parent view controller is normally used for UIViewControllerContainment protocol conformance. UINavigationController does not conform to that protocol, which makes me think you're not going to be able to remove it from the navigation controller by calling that method. You can use – setViewControllers:animated: on your navigationController to assign the controllers. Just use the array you created with all the viewControllers.
If you are using a pop, you don't have to remove the other view from it's parent view. You can just pop to a view controller that is in the view controller array and back two from the top of the stack.
I would give a code example, but i'm not on my dev computer. If nobody has answered with more detail before tomorrow, I will add some code.
-Edit - Here is the line of code that I use to pop back 2 view controllers. I'm sure this could be done better ways, but it works.
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:[self.navigationController.viewControllers count]-3] animated:YES];
I have two views which use UINavigationController. I want the second view to "slide" with animation back to the first view when I tap on a custom button. (not the original in navigationbar)
Try something like this on that button action,
[self.navigationController popToViewController:aViewController animated:YES];
or
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] - 2)] animated:YES];
Here aViewController represents the previous view controller. Similarly you can pop to any viewcontroller you want by using a similar code.
[self.navigationController popToRootViewControllerAnimated:YES]; will take you to the root view controller.
This question already has answers here:
How do I pop two views at once from a navigation controller?
(18 answers)
Closed 9 years ago.
Is it possible to pop the navigation controller twice? I have this navigation structure:
View One ----> View Two -----> View Three
What I'd like to accomplish is that by the tap of a row on View Three, go back directly to View One. I've done it from Three to Two via protocol-delegate, but setting the delegate in view One doesn't work and setting two consecutive delegate-protocol both poping the navigation controller, gives me error: nested navigation controller activity (or something similar).
Any help would be appreciated. Thanks in advance!
There is a few pop options
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
The first pops the top controller.
The second allows you to pop the whole stack off to get to the root.
The third allows you to pop to any viewController you have a reference to. You can get the viewController with self.navigationController.viewControllers and then work with the array to get the specific viewController you want to pop to
Use the following code. You can use any number instead of -3 to pop to a different level.
Obj-C:
ViewController *View = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3];
[self.navigationController popToViewController:View animated:YES];
Swift 3.0*:
let controller = self.navigationController?.viewControllers[(self.navigationController?.viewControllers.count)! - 3]
self.navigationController?.popToViewController(controller!, animated: true)
You can try this
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:1] animated:YES];
Hope it Helps!!
'self' seems to be released after first pop
UINavigationController *navigationController = self.navigationController;
[navigationController popViewControllerAnimated:NO];
[navigationController popViewControllerAnimated:YES];