user3085646
user3085646

Reputation: 1055

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];
    };

Upvotes: 7

Views: 12535

Answers (3)

user1664018
user1664018

Reputation: 81

//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];
};

Upvotes: 0

Gil Sand
Gil Sand

Reputation: 6040

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

Upvotes: 0

FreeXu
FreeXu

Reputation: 27

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.

Upvotes: 2

Related Questions