How to implement the correct uinavigationbar button back function

I want to go back to the previous view controller, when I click the cancel button of my search display controller, I used the code:

[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex:0] animated:YES]; 

but this gives me the following error:

* Application termination due to the uncaught exception "NSInternalInconsistencyException", reason: the "search content" of the navigation controller should not change between -setActive: YES and -SETACTIVE: NO '

+4
source share
5 answers

[self.navigationController popViewControllerAnimated: YES];

will return you to the previous view controller that was laid out in this navigation controller. it should do it.

+4
source

My problem was that the searchdisplay controller cannot install Active using uinavigator, so I solve my problem by adding the following code:

[self.searchDisplayController setActive: NO];

[self.navigationController popViewControllerAnimated: YES];

Error:

* Approval error in - [UISearchDisplayController setActive: animated:], / SourceCache / UIKit_Sim / UIKit-1914.84 / UISearchDisplayController.m: 617 2012-05-13 19: 07: 44.696 MyApp [3648: 11903] * Application termination due to uncaught exceptions "NSInternalInconsistencyException", reason: "the content navigation controller should not change between -setActive: YES and -setActive: NO '

+2
source

I had the same problem and found a solution.

The trick is not to give searchDisplayController to call -setActive:animated: anywhere, but in -viewWillDisappear . Thus, you should make your view controller a delegate from searchBar (to catch the cancel button) and lay the hidden button in the background when searchDisplayController become active (to capture taps on an empty screen space).

In your view, the controller implements these methods:

 - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.searchDisplayController setActive:NO animated:animated]; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { [self.navigationController popViewControllerAnimated:YES]; } - (void)backgroundClicked:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button addTarget:self action:@selector(backgroundClicked:) forControlEvents:UIControlEventTouchUpInside]; CGFloat searchBarHeight = controller.searchBar.frame.size.height; button.frame = CGRectOffset(self.view.bounds, 0.f, searchBarHeight) ; [self.view addSubview:button]; } 
+2
source

You do not need a popToViewController, if you only need to return to the previous view, just add the "Back" button on the navigation controller and you can click it to return: -

 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = backButton; [backButton release]; 

or if you really want to use this view, use this: -

 UINavigationController *navController = self.navigationController; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration: 0.7]; [UIView setAnimationTransition:<#UIViewAnimationTransitionCurlDown#> forView:navController.view cache:NO]; [navController popViewControllerAnimated:NO]; [UIView commitAnimations]; 
0
source

The object at index 0 is the root view controller. so this is the first object on the stack. but you can only put the last object. Instead of using

 [self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex:0] animated:YES]; 

using

 [self.navigationController popToViewController: [self.navigationController.viewControllers lastObject] animated:YES]; 

or

 [self.navigationController popToViewControllerAnimated:YES]; 
0
source

Source: https://habr.com/ru/post/1412232/


All Articles