How to use the back button using currentViewController?

Here is my code to call the new view controller, which is wrapped in a UINavigationController. I want a simple return button in the restaurant manager. My selector doesn't seem to work. I tried using pop commands. Will they work as long as I use presentViewController and not some kind of push?

Pretty sure my selector is wrong right now because it says self.navigationController, which may not be right.

Here I invoke the new view controllers and configure 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]; }; 
+7
ios objective-c xcode
source share
3 answers
 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil ction:@selector(backPressed:)]; restaurantResults.navigationItem.leftBarButtonItem = backButton; 

This code should be used in restaurants ViewController; the goal is me.

+2
source share

// It works - just did // You can create a back button in the view you want to see the 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 reject the view controller in the view controller, where the return button is as below-

 - (void)backPressed:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; }; 
0
source share

If you use the UINavigationController and you need the default iOS return button, you do not need to install it programmatically. There is literally nothing to add, it is built in by default.

By inserting the UIViewController into the navigation controller, it will end up on the navigation controller stack, and so iOS will add the navigation bar and back button when you dig into the navigation stack.

If this does not work, check the following:

  • That the controller you are working with is the root of the UINavigationController. You can set this by code or in a storyboard. (are you okay)

  • What you click from the navigation controller, not from the view controller. Essentially, you should do navigationcontroller.push() and not self.push() , otherwise it just won't work. (depends on what self here, but I'm sure your error is here)

I see that you are using presentViewController which is designed for modal presentViewController , it’s good if this is your intention, but if you need a navigation stack, why not first embed self in the navigation controller, hide its navigation bar and then just paste your next one on it controller.

Thus, you do not need to manually create this button back and let iOS handle everything.

If you have to do it this way, you can “reject” only when you “imagine” and “pop out” when you “push”. But I don’t have enough information to understand why yours is not working. Try a few things and give us more feedback. But from what I see, you are moving towards a more complex solution than necessary.

In addition, I would really start with a simple button that says “close” and see if it works that way before trying to embed it in the panel with the item. This way you solve one problem and one new concept at a time

0
source share

All Articles