我正在尝试自定义应用程序导航栏的行为。每次按下后退按钮,我都设法将UINavigationController子类化以运行自定义代码,但是,导航栏标题仍会更改,并且再次按下该按钮会忽略我的代码(也许是因为它和整个控制器不同?)。

我要实现的是告诉UINavigationController在某些条件下(已从自定义“导航控制器”定义并由控制器进行控制)忽略对后退按钮的按下操作,从而使当前视图和导航栏保持完整。

我该如何实现?我只覆盖了(UIViewController *)popViewControllerAnimated:(BOOL)animated函数。

我的代码:

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    if(self.auxController == nil)
    {
        NSLog(@"[CustomNavigationController.m]: WARNING: No Auxiliar Controller assigned to Navigation controller");
        return [super popViewControllerAnimated:animated];
    }
    else
    {
        NSLog(@"[CustomNavigationController.m]: Navigation Stack has %d remaining levels",[[[self auxController] parentLayout] count]);
        if([[[self auxController] parentLayout] count] > 0)
        {
            NSLog(@"[CustomNavigationController.m]: Falling to previous navigation level");
            [[self auxController] navBack];
        }
        else
        {
            NSLog(@"[CustomNavigationController.m]: No more navigation levels in stack, falling to previous view");
            self.auxController = nil;
            return [super popViewControllerAnimated:animated];
        }
    }
    return nil;
}

最佳答案

正如@ Stonz2在评论中提到的,最干净的解决方案是使用您自己的自定义后退按钮。
只需在导航控制器中隐藏“后退”按钮,然后设置自己的左BarButtonItem。在此BarButtonItem中,您可以手动返回,也可以不执行任何操作。

用代码说话:

UIBarButtonItem * customBackButton = [UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStylePlain  target:self action:@selector(backButtonPressed:)]
navigationItem.hidesBackButton = true;
navigationItem.leftBarButtonItem = customBackButton;


不要以为有任何更清晰的东西!

10-06 13:25
查看更多