76

我需要检测用户何时点击导航栏上的“返回”按钮,以便在发生这种情况时执行一些操作。我正在尝试以这种方式手动设置此类按钮的操作:

[self.navigationItem.backBarButtonItem setAction:@selector(performBackNavigation:)];

- (void)performBackNavigation:(id)sender
{
   // Do operations

   [self.navigationController popViewControllerAnimated:NO];
}

我首先将该代码放在视图控制器本身中,但我发现它self.navigationItem.backBarButtonItem似乎是nil,所以我将相同的代码移动到父视图控制器,它将前者推送到导航堆栈。但我也无法让它发挥作用。我已经阅读了一些关于这个问题的帖子,其中一些人说选择器需要在父视图控制器上设置,但对我来说它无论如何都不起作用......我做错了什么?

谢谢

4

10 回答 10

131

尝试使用此代码VIewWillDisappear来检测是否按下 NavigationItem 的后退按钮:

-(void) viewWillDisappear:(BOOL)animated
{
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) 
    {
        // Navigation button was pressed. Do some stuff 
        [self.navigationController popViewControllerAnimated:NO];
    }
    [super viewWillDisappear:animated];
}

或者还有另一种方法来获取导航返回按钮的操作。

为后退按钮的 UINavigationItem 创建自定义按钮。

例如:

在 ViewDidLoad :

- (void)viewDidLoad 
{
    [super viewDidLoad];
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(home:)];
    self.navigationItem.leftBarButtonItem=newBackButton;
}

-(void)home:(UIBarButtonItem *)sender 
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

斯威夫特:

override func willMoveToParentViewController(parent: UIViewController?) 
{
    if parent == nil 
    {
        // Back btn Event handler
    }
}
于 2013-09-16T09:12:11.840 回答
41

迅速

override func didMoveToParentViewController(parent: UIViewController?) {
    if parent == nil {
        //"Back pressed"
    }
}
于 2014-12-16T12:59:37.713 回答
18

也许这个答案不符合您的解释,而是问题标题。当您试图知道您何时点击了UINavigationBar.

在这种情况下,您可以使用UINavigationBarDelegate协议并实现以下方法之一:

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;

调用方法时didPopItem,这是因为您点击了后退按钮或使用[UINavigationBar popNavigationItemAnimated:]了方法,并且导航栏确实弹出了该项目。

现在,如果您想知道是什么动作触发了该didPopItem方法,您可以使用标志。

使用这种方法,我不需要手动添加带有箭头图像的左栏按钮项,以使其类似于 iOS 后退按钮,并且能够设置我的自定义目标/操作。


让我们看一个例子:

我有一个视图控制器,它有一个页面视图控制器和一个自定义页面指示器视图。我还使用自定义 UINavigationBar 来显示标题以了解我在哪个页面上,并使用后退按钮返回上一页。而且我还可以在页面控制器上滑动到上一页/下一页。

#pragma mark - UIPageViewController Delegate Methods
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {

    if( completed ) {

        //...

        if( currentIndex > lastIndex ) {

            UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Some page title"];

            [[_someViewController navigationBar] pushNavigationItem:navigationItem animated:YES];
            [[_someViewController pageControl] setCurrentPage:currentIndex];
        } else {
            _autoPop = YES; //We pop the item automatically from code.
            [[_someViewController navigationBar] popNavigationItemAnimated:YES];
            [[_someViewController pageControl] setCurrentPage:currentIndex];
        }
    }

}

那么我实现 UINavigationBar 委托方法:

#pragma mark - UINavigationBar Delegate Methods
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
    if( !_autoPop ) {
        //Pop by back button tap
    } else {
        //Pop from code
    }

    _autoPop = NO;

    return YES;
}

在这种情况下,我使用shouldPopItem了因为弹出是动画的,我想立即处理后退按钮,而不是等到转换完成。

于 2014-01-30T16:32:41.667 回答
12

它的问题didMoveToParentViewController在于,一旦父视图再次完全可见,它就会被调用,所以如果你需要在此之前执行一些任务,它就无法工作。

它不适用于驱动动画手势。使用willMoveToParentViewController效果更好。

目标-c

- (void)willMoveToParentViewController:(UIViewController *)parent{
    if (parent == NULL) {
        // ...
    }
}

迅速

override func willMoveToParentViewController(parent: UIViewController?) {
    if parent == nil {
        // ...  
    }
}
于 2015-08-06T10:51:56.510 回答
6

这是达达奇答案的 Objective-C 版本:

Objective-C

- (void)didMoveToParentViewController:(UIViewController *)parent{
    if (parent == NULL) {
        NSLog(@"Back Pressed");
    }
}
于 2015-07-14T16:54:38.593 回答
3

设置 UINavigationBar 的委托,然后使用:

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
    //handle the action here
}
于 2014-07-24T06:28:37.403 回答
3

其他解决方案都不适合我,但这确实:

创建您自己的 UINavigationController 子类,使其实现 UINavigationBarDelegate(无需手动设置导航栏的委托),添加 UIViewController 扩展,该扩展定义要在按下后退按钮时调用的方法,然后在您的 UINavigationController 子类中实现此方法:

func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
    self.topViewController?.methodToBeCalledOnBackButtonPress()
    self.popViewController(animated: true)
    return true
}
于 2018-12-05T23:18:33.360 回答
3

在 Swift 4 或更高版本中:

override func didMove(toParent parent: UIViewController?) {
    if parent == nil {
        //"Back pressed"
    }
}
于 2019-05-10T10:06:19.027 回答
2

设置 UINavigationControllerDelegate 并实现这个委托函数(Swift):

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    if viewController is <target class> {
        //if the only way to get back - back button was pressed
    }
}
于 2015-08-26T15:23:34.133 回答
1

使用UINavigationController实现该shouldPop方法的自定义子类。

在斯威夫特:

class NavigationController: UINavigationController, UINavigationBarDelegate
{
    var shouldPopHandler: (() -> Bool)?

    func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool
    {
        if let shouldPopHandler = self.shouldPopHandler, !shouldPopHandler()
        {
            return false
        }
        self.popViewController(animated: true) // Needed!
        return true
    }
}

设置后,shouldPopHandler()将调用您来决定控制器是否弹出。如果未设置,它将像往常一样弹出。

UINavigationController禁用s是个好主意,interactivePopGestureRecognizer因为否则手势不会调用您的处理程序。

于 2018-12-20T14:14:07.347 回答