Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
    };
like image 587
user3085646 Avatar asked Dec 26 '13 23:12

user3085646


1 Answers

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.

like image 163
FreeXu Avatar answered Sep 23 '22 04:09

FreeXu