4

当我单击搜索显示控制器的取消按钮时,我想返回上一个视图控制器,我使用了以下代码:

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

但它给了我以下错误:

*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“搜索内容导航控制器不得在 -setActive:YES 和 -setActive:NO 之间更改”

4

5 回答 5

4

[self.navigationController popViewControllerAnimated:YES];

将带您回到堆叠在此导航控制器中的上一个视图控制器。那应该这样做..

于 2012-05-13T10:05:51.743 回答
2

I had the same problem and found a solution.

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

In your view controller implement 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];
}
于 2012-12-27T00:08:54.750 回答
2

我的问题是 uinavigator 无法设置 searchdisplay 控制器,所以我通过添加以下代码来解决我的问题:

[self.searchDisplayController setActive:NO];

[self.navigationController popViewControllerAnimated:YES];

错误是:

* 断言失败 -[UISearchDisplayController setActive:animated:], /SourceCache/UIKit_Sim/UIKit-1914.84/UISearchDisplayController.m:617 2012-05-13 19:07:44.696 MyApp[3648:11903] *由于未捕获的异常而终止应用程序'NSInternalInconsistencyException',原因:'搜索内容导航控制器不得在 -setActive:YES 和 -setActive:NO 之间更改'

于 2012-05-13T18:23:36.593 回答
0

如果您只需返回上一个视图,则不需要 popToViewController,只需在导航控制器上添加一个后退按钮,您可以单击它返回:-

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

或者,如果您真的想弹出视图,请使用:-

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];
于 2012-05-13T09:15:12.517 回答
0

索引 0 处的对象是根视图控制器。所以它是堆栈中的第一个对象。但你只能弹出最后一个对象。而不是使用

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

利用

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

或者

[self.navigationController popToViewControllerAnimated:YES];
于 2012-05-13T10:01:12.660 回答