How to back to rootViewController - ios

How can I go back from 2nd view controller when I click on UIBarButtonItem using the custom segue?
UIBarButtonItem *backButton=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"back.png"] style:UIBarButtonItemStylePlain target:self action:#selector(back)];
I have used the following code for:
- (IBAction)back {
UIViewController *source=(UIViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"MainView"];
[self.navigationController popToViewController:source animated:YES];
}

instead of second line you can write:
[self.navigationController popToRootViewControllerAnimated:YES];

As other people say, you should use for your case:
[self.navigationController popToRootViewControllerAnimated:YES];
I want add that the method that you used, the popToViewController you should use it for to return to one specific view controller. In particular, this method pop the view controller on top of the navigation stack; you can check this and similar method for control the navigation in Apple Documentation.

Related

How to navigate to some other View Controller when Navigation Controller back button is pressed?

I have a viewController in which, on the click of back button, I need to go to a specific viewController and call its method.So I created a barButton and added as a BACK button in navigation bar.When its selector is called I can see only see a black screen, nothing else.
Here how I am doing it.
In viewDidLoad
//Back Button in navigation Bar.
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(navigationBackButtonClicked:)];
self.navigationItem.leftBarButtonItem=newBackButton;
The selector below executes and shows a black screen.
-(void)navigationBackButtonClicked:(UIBarButtonItem *)sender {
SharedManager *sharedManagerObject = [SharedManager sharedInstance];
NSString *source = sharedManagerObject.toCityString;
NSString *destination = sharedManagerObject.fromCityString;
NSString *dateStr = sharedManagerObject.dateSelected_String;
BusListViewController *buslist_VC = [self.storyboard instantiateViewControllerWithIdentifier:#"BusListViewController"];
[buslist_VC getBusListForSource:source destination:destination date:dateStr];
[self.navigationController popToViewController:buslist_VC animated:YES];
}
You need to add your buslist_VC to the view hierarchy of your navigation controller before using [popToViewController:animated:]. This is used to display some viewcontrollers already in the stack of your navigation Controller.
Either way what you're asking might be a weird behaviour for your user but you can use :
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 1] animated:NO];
[self.navigationController pushViewController:buslist_VC animated:NO];
You cann't back to a new ViewController, you can push a new ViewController, in your code change this:
[self.navigationController pushViewController:buslist_VC animated:YES];
But keep in mind, you are putting this new viewController inside the other (where you are created a newBackButton), and the default back button come back to this. If you want to come back to the root use this:
[self.navigationController popToRootViewControllerAnimated:YES];
Or, now you can come back to any viewController on the navigation stack, this array:
NSArray *navStacks = self.navigationController.viewControllers;
Using popToViewController: be sure is in the stack.
You should use
NSArray *navStacks = self.navigationController.viewControllers;
select needed view controller and do
[self.navigationController popToViewController:selectedVC animated:YES];

xCode Navigation Back Button not popping detailview

Whenever I press on the 'go back' button on my final detail view, it wont go back to the table view.
I have a storyboard setup like this:
UITabViewController -> UINavigationController -> UITableView -> UINavigationController -> UIView (The detailview).
When I run my program I see the back button, but clicking it does nothing.
Here is the code for DetailViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *backbtn = [[UIBarButtonItem alloc] initWithTitle:#"Go Back" style:UIBarButtonItemStylePlain target:self action:#selector(gobackBtn)];
[self.navigationItem setLeftBarButtonItem:backbtn];
[self configureView];
}
- (void)gobackBtn
{
[self.navigationController popViewControllerAnimated:YES];
}
As if [self.navigationController popViewControllerAnimated:YES]; does nothing.
I'm out of ideas. Why does the backbutton not 'pop' the detailview?
Your detail view controller is the root view controller of the navigation controller. So 'popping' does nothing. You need instead to dismiss the navigation controller. Technically, you should use a delegate to tell your TableViewController to dismiss it, but I think
[self.navigationController dismissViewControllerAnimated:YES completion:NULL];
from within your goBackButton method of your detail view controller will do the trick.

not able to move to another view controller

I'm trying to move from 1st view controller -> 2nd view controller. However, it seems nothing is happening.
It is mainly from a sign-in page, so I don't need to get back to that page once I have a successful login. I'm trying right now from a button just to check.
I have 2 classes.
signInPage
optionsScreen
I need to go from 1 > 2
Inside signInPage.m
#import "optionsScreen.h"
- (IBAction)moveToNext:(id)sender {
optionsScreen *aSecondPageController =
[[optionsScreen alloc]
initWithNibName:#"optionsScreen"
bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:YES];
// [aSecondPageController release];
}
Screenshot of story board
https://drive.google.com/file/d/0B1y7ao4cGAYKaU0yRlVxcVg2bzA/edit?usp=sharing
Solution:
- (IBAction)moveToNext:(id)sender {
NSString * storyboardName = #"Main_iPhone";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"optionsScreen"];
[self presentViewController:vc animated:YES completion:nil];
}
In storyboard add navigation view controller. As root view controller - perhaps by default it is table view controller, so remove it - set up 1st view controller. Adding navigation controller you can push to navigation stack as many view controllers as you want.
I think what you're trying to do is present the view controller modally, not push it onto the stack (which doesn't exist). Instead you should just do this. This will not show a back button by default so you'll have to add one manually:
optionsScreen *aSecondPageController = [[optionsScreen alloc] initWithNibName:#"optionsScreen" bundle:nil];
[self presentViewController:aSecondPageController animated:YES completion:nil];
To go back, in your optionsScreen view controller, you'll need some sort of back button right? So you can do this in your viewDidLoad:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backPressed:);
Then you'll have to create a new method called backPressed:
-(void)backPressed:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
Edit: it turns out you were trying to do what I explained above. I will leave the navigation controller bit for future reference.
If you are trying to use a navigation controller to push your options, which will give you a back button by default, then first, in your storyboard, click your sign in view controller, then at the menu bar at the top go to Editor > Embed In... > Navigation Controller. Then in your view controller switch method, you can do what you were trying before
optionsScreen *aSecondPageController = [[optionsScreen alloc] initWithNibName:#"optionsScreen" bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:YES];

how to use back button with presentViewController?

Here is my code for calling a new view controller that is wrapped in a UINavigationController. I want a simple back button on the restaurantResults controller. My selector does not seem to work. I've tried using pop commands. Will those work since I am using presentViewController and not any sort of push?
Pretty sure my selector is wrong right now because it says self.navigationController, which can't possibly be right.
Here is where I call the new view controllers and set up the back button:
- (void)searchBarSearchButtonClicked:(UISearchBar *)foodNearSearchBar
{
restaurantsViewController *restaurantResults = [[restaurantsViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:restaurantResults];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(backPressed:)];
restaurantResults.navigationItem.leftBarButtonItem = backButton;
[self presentViewController:navController animated:YES completion:Nil];
}
Here is my selector:
-(void)backPressed: (id)sender
{
[self.navigationController popViewControllerAnimated: YES]; // or popToRoot... if required.
}
I also tried:
- (void)backPressed:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
};
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
ction:#selector(backPressed:)];
restaurantResults.navigationItem.leftBarButtonItem = backButton;
these code should be used on the restaurantsViewController;
the target is self.
If you are using a UINavigationController and you want the default iOS back button, you do not need to set it programatically. There is literally nothing to add, it's built in by default.
By pushing the UIViewController into the navigation controller, it'll be in the navigation controller stack of controllers, and iOS will therefore add a navigation bar, and a back button when you dig into the navigation stack.
If its not working, check the following :
That the controller you're pushing from is the root of the UINavigationController. You can set this by code or in storyboard. (you're OK)
That you're pushing from the navigation controller and not the viewcontroller. Essentially you have to do navigationcontroller.push() and not self.push(), otherwise it just won't work. (depends what self is here, but I'm pretty sure your mistake is here)
I see that you're using presentViewController which is for modals, its fine if that is your intent, but if you want a navigation stack, why not embed self in a navigation controller in the first place, hide its navigation bar, and then simply push your next controller onto it.
That way you dont have to manually create that back button, and let iOS deal with everything.
If you must do it that way, you can only "dismiss" when you "present", and "pop" when you "push". But I don't have enough information to know why yours does not work. Try a few things and give us more feedback. But from what I see you're going for a more complicated solution than necessary.
Also I would really start with a simple button that says "close" and see if it works that way before trying to embed it in a bar with an item. That way you tackle one problem, and one new concept at a time
//This works - Just did it
//You can create back button in the view you want to see back button -
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Login"
style:UIBarButtonItemStylePlain
target:self
action:#selector(backPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
[self.navigationItem setHidesBackButton:NO];
// And just do dismiss the view controller in the viewcontroller where the back button is - as below-
- (void)backPressed:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
};

Custom back button selector not triggered

I have two UIViewcontrollers, let's call them vcA and vcB in an UINavigationController.
I want vcB to have a custom backbutton that triggers some code, the goal is do some custom animation
In vcA I put this code:
UIViewController *vcB = [UIViewController alloc] init]
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(handleBack)];
self.navigationItem.backBarButtonItem = custombackBackButton;
[self.navigationController pushViewController: vcB animated: YES];
Then I added this code both in vcA and vcB:
-(void) handleBack
{
NSlog(#"Going back to vcA");
}
The handleback method is never called. Any hint?
Thanks
Nicola
Dont get me wrong but i think you need to be clear about push pop of UINavigationController.
A navigation controller manages views by pushing/popping them on/off the controller's view stack. When you push an item, the current view slides off screen to the left, and the new view slides over from the right. Ofcourse these animations can be changed according to your wish.
I think This is what you need completely.
Put this in vcA where you want to push vcB from vcA.
UIViewController *vcB = [UIViewController alloc] init];
[self.navigationController pushViewController: vcB animated: NO];
[UIView transitionWithView:self.navigationController.view
duration:0.8
options:UIViewAnimationOptionTransitionFlipFromRight
animations:nil
completion:nil];
In vcB, you can make a barbuttonitem
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:#"BackToVcA"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
In addActionMethod: you can put the below code for navigating back with your required animation
- (void)addAction:(id)sender
{
UIViewController *vcA = [UIViewController alloc] init];
[[self retain] autorelease];
[self.navigationController pushViewController: vcA animated: NO];
[UIView transitionWithView:self.navigationController.view duration:0.8 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil];
[self.navigationController popViewControllerAnimated:NO];
}
Use trick given by William Jockusch Setting action for Back Button
And also As per the Updating the Navigation Bar
If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.
If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has a valid item in its backBarButtonItem property, the navigation bar displays that item.
So if you want to have custom selector You need to write this inside vcB NOT IN vbA
vcB.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(handleBack)];
self.navigationItem.backBarButtonItem = customBackButton;
}
-(void) handleBack
{
NSlog(#"Going back to vcA");
}
NOTE:
UINavigationController Class Reference
When this navigation item is immediately below the top item in the
stack, the navigation controller derives the back button for the
navigation bar from this navigation item. When this property is nil,
the navigation item uses the value in its title property to create an
appropriate back button. If you want to specify a custom image or
title for the back button, you can assign a custom bar button item
(with your custom title or image) to this property instead. When
configuring your bar button item, do not assign a custom view to it;
the navigation item ignores custom views in the back bar button
anyway.
Here is is mentioned that If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) but it ignores custom view. So your selector is not invoking.
Your doing mistake. First Push to vcB and in vcB viewDidLoad method put this code.
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(handleBack)];
self.navigationItem.backBarButtonItem = custombackBackButton;
and also put this in vcB:
-(void) handleBack{
NSlog(#"Going back to vcA");
}
you can not modify the backBarButtonItems action , it is do the default (back) action , you should do your custom thing in the leftBarButtonItems
in your vcBs class add this :
- (void)viewWillAppear:(BOOL)animated
{
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(handleBack)];
self.navigationItem.leftBarButtonItem = customBackButton;
}
- (void)handleBack
{
NSLog(#"back");
}

Resources