如何实现正确的uinavigationbar后退按钮功能

时间:2012-05-13 09:05:45

标签: iphone objective-c ios

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

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

但是它给了我以下错误:

  

* 由于未捕获的异常'NSInternalInconsistencyException'而终止应用,原因:'搜索内容   导航控制器不能在-setActive:YES和之间切换   -SETACTIVE:否'

5 个答案:

答案 0 :(得分:4)

[self.navigationController popViewControllerAnimated:YES];

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

答案 1 :(得分:2)

我的问题是搜索显示控制器无法通过uinavigator setActive所以我通过添加以下代码来解决我的问题:

[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'

答案 2 :(得分:2)

我遇到了同样的问题并找到了解决方案。

诀窍不是让searchDisplayController-setActive:animated:的任何地方拨打-viewWillDisappear。 所以你应该让你的视图控制器成为searchBar的代表(以点击“取消”按钮)并在searchDisplayController变为活动状态时在背景上放置一个隐藏按钮(以捕捉空屏幕空间上的点击) )。

在视图控制器中实现以下方法:

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

答案 3 :(得分:0)

如果您只需要返回上一个视图,只需在navigationcontroller上添加一个后退按钮,您就可以单击它返回: -

你不需要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];

答案 4 :(得分: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];