Tiois
Tiois

Reputation: 1559

Cancel backButton click in my navigationController

I have a custom navigationController :

#import "customNavigationController.h"
#import "StartViewController.h"
#import "EtapeViewController.h"

@implementation customNavigationController

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    // Accueil du guide, on cache le navigationBar
    if([self.viewControllers count] == 2){
        self.navigationBarHidden = TRUE;
        return [super popViewControllerAnimated:animated];
    }
    // Si on est pas à l'accueil, on fait l'action normal sur le backBarButton
    else {
        // Si on est dans une étape, le backButton va servir à reculer dans les étapes, et non reculer vers le workflow
        NSString *className = NSStringFromClass([[self.viewControllers objectAtIndex:[self.viewControllers count] - 1] class]);
        if ([className isEqualToString:@"EtapeViewController"]) {
            EtapeViewController *etape = [self.viewControllers objectAtIndex:[self.viewControllers count] - 1];
            if (etape.show_previous_button) {
                [etape previousEtape:nil];
                return FALSE;
            }
            return [super popViewControllerAnimated:animated];
        }
        else {
            return [super popViewControllerAnimated:animated];
        }
    }
}
@end

In some case, I want to cancel the click event of the backButton (on the line that reads "return FALSE"), but it doesn't work.

Is there a way to do it?

Upvotes: 0

Views: 627

Answers (3)

skue
skue

Reputation: 2077

I don't understand why you would make the Back button ignore taps? It seems like this would confuse users and the App Store team would consider this a bug. Perhaps you could you post a screenshot?

It would probably be better to redesign your interface and consider 1) using toolbar buttons for navigation (like Mobile Safari) or 2) fully support UINavigation based views rather than working around it.


Update: It sounds like you're going to perform a different action, like displaying a confirmation? I don't know of any official ways to do what you want, since the UINavigationControllerDelegate methods just notify you about transitions, they don't let you cancel/modify them. (And if the transition is animated then playing with the navigation controller's view stack probably won't help.)

So you could always float a transparent (or almost transparent) window over the back button and intercept taps that way. Here's some sample bar that does something similar with the status bar:

https://github.com/myell0w/MTStatusBarOverlay

Upvotes: 0

amattn
amattn

Reputation: 10065

in place of return FALSE, you can do:

return nil;

or

return [self topViewController];

Either should have the right side effect.

That being said, be careful with your UI design here. Make sure the user knowns why the back button doesn't work somehow.

Upvotes: 1

onnoweb
onnoweb

Reputation: 3038

Why don't you disable the back button in the situations that you don't want the user to tap it?

Upvotes: 0

Related Questions