My app has a main view with a few buttons lined up vertically. Each button is linked to a different view controller. Now, when the user taps on the first button, the first view controller is opened. Within the first view controller, there is a button which takes you directly to the second view controller. When I get to the second view controller, and press the back button, I am taken back to the first view controller. Then I have to press the back button one more time to be taken to the main view controller. What can I do to make the app take me directly to the main view controller from the second view controller, and not through the first view controller? Thanks in advance!
You could bypass the segue_unwind to go back to the main view controller. Just call the performSegueWithIdentifier in the IBAction of the button
Swift
self.performSegueWithIdentifier("SegueIdentifierName", sender: self)
Objective C
[self performSegueWithIdentifier:#"SegueIdentifierName" sender:self];
Here is a similar article as well
What are Unwind segues for and how do you use them?
You can override the back button action to call popToViewController() on the navigationController with the view controller you would like to return to.
There are a lot of answers in SO about this BUT:
[self.navigationController popToRootViewControllerAnimated:YES];
OR
UIViewController *rootViewController = [self.navigationController.viewControllers firstObject];
[self.navigationController pushViewController:rootViewController animated:YES];
Should work just fine.
Next time, TRY to find answers first:
How do I get the RootViewController from a pushed controller?
SWIFT 3.01
self.performSegue(withIdentifier: "SegueIdentifierName", sender: self)
Related
Here is the scenario: I have 2 views, each embedded in a navigation controller. The first nav bar has 1 edit nav bar item, when tapped it goes to the 2nd VC. The 2nd VC nav bar has a cancel and save button, and when tapped should respond accordingly. I want to code the cancel action first, which will just call [self dismissViewControllerAnimated:YES completion:nil] but it is not doing anything...
I have also tried [self.navigationController popViewControllerAnimated:YES] and neither works, please help
BTW both segues are push segues
sounds like something off in you segues ...
i did a quick test in the following way:
create two view controllers in the story board ,each has it own navigation controller.
crate a segue from the first view controller to the second one.
2 in the first view controller i added a bar button that preform the segue from 2.
3 to the second view controller i added a bar button with an action that did : popViewControllerAnimated:YES..
all works fine...
though i don't really understand why you need 2 navigation controllers here....
This may be a simple answer, but I am new to this... my back button in the navigation bar is confused and does not want to go back to the Main Menu. Instead, it goes back to the previous view. How can I specify in my app that the "back" button should always go to to a specific view?
All you have to do is create a UIBarButton and connect an outlet to it. In the action:
-(IBAction)MainMenu
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
If you want to go back to the root controller:
popToRootViewControllerAnimated:
Pops all the view controllers on the stack except the root view controller and updates the display.
[self.navigationController popToRootViewControllerAnimated:YES];
If you want to go back to an specific controller:
popToViewController:animated:
Pops view controllers until the specified view controller is at the top of the navigation stack.
You will have to create a new outlet in your view controller to handle the button press. Then use the code NSElvis posted in his answer to pop to root.
in the top view of my view controller (the last table view controller) has an add navigation item. i added a view controller object from the objects library and i ctrl + dragged from the plus button to the view controller. i tried the app and it works fine but i can't go back to the previous controller when i reach the last controller. since the last controller connected (by segue) to the plus button, i can't have a navigation bar on top. so i added one and added an navigation item called it Done. i created an IBAction method in the class that the last controller subclasses which have the following code:
[self.navigationController popNavigationControllerAnimated:YES];
However, when i run the app and press the Done button to go back, it doesn't work although i feel like what i did is totally legal.
If you would have made the final view controller segue a Push segue, you'd still have the navigation bar with a back button. It makes sense since you're adding a record that you'd want a modal view.
You can dismiss the current modal view with the following code:
[self dismissViewControllerAnimated:YES completion:nil];
Generally, you should use delegation and dismiss it from the presenting view controller. However, I think it's fine to dismiss yourself if you're using storyboards, segues, and ARC.
Did you create a Bar Button Item and assigned its 'selector' callback?
I'm using the following code to perform a segue to another view controller:
[self performSegueWithIdentifier:#"BackSegue" sender:self];
This works fine when the destination of "BackSegue" named segue is another view controller (one of the tabs, actually) but I need to display the tabs at the bottom so transitioning directly to this view controller won't work as there will be no tabs. Is it allowed/possible to segue to a tabbar controller? Is anything wrong with this specific code or would it be something else I'm doing?
Edit 1
The TabBar controller has no .m/.h files and is never declared programmatically, but I'm pretty sure the segue is set up correctly in the storyboard to the best of my knowledge (the same way it was set up earlier directly to the other viewcontroller).
You can segue directly to a UITabBarController. Just change the segue in your storyboard. When the segue occurs, it should load the tab bar controller, and consequently the tab bar at the bottom of the screen and the first view controller's view associated with the tab bar controller.
In order to segue to the specific tab in the tab bar controller:
You need to add the selectedIndex=1
Add these lines of code for segue:
UITabBarController *loadTabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TasksAppsTabs"];
loadTabBar.selectedIndex=1;
[self presentViewController:loadTabBar animated:YES completion:nil];
my situation is as follow:
1) i have rootViewController with navigationController and add button in the toolbar. When i press i push another view using pushViewController method. This view called chooseTypeView which has only tableview with two cells.
2) when any cell is clicked third view will be pushed using the same method to enter some data.
3) now i want when i press the "Done" button in the keyboard to navigate back to rootView controller and dismiss all views in between the current steps and the root view.
i'm using #Protocol to connect views and i could pass information from the last view to the root view but i couldn't dismiss it.
Thanks for all and i hope that i make myself clear.
I could answer this question my self.
In the delegate method i can call [self.navigationController popViewControllerAnimated:YES] which will remove the current view from navigationController views stack.
For SWIFT use the below code:
self.navigationController.popViewControllerAnimated(true)