简体   繁体   中英

Catching UINavigationController's native back button action

There are many questions about overriding navigation controller's back button action, but everybody tells that there is no direct way to do this. I've implemented this by inheriting UINavigationController and overriding UINavigationBarDelegate method. But the problem is that interface of UINavigationController doesn't implement UINavigationBarDelegate protocol (but it implements this method, because this code works:), and I afraid that app will be rejected by Apple, considering that here is used undocumented API.

So, the question is: how do you think, is it private API usage or not? Here is the code:

@interface CustomNavigationController : UINavigationController <UINavigationBarDelegate>

@end

@implementation CustomNavigationController

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
    BOOL shouldPop = [[self topViewController] enableBackButton]; // every view controller overrides this method to disable or enable pop of view controller
    if (shouldPop) {
        //here is the warning about "UINavigationController may not respond to selector"
        return [super navigationBar:navigationBar shouldPopItem:item];
    } else {
        return NO;
    }
}

@end

Apple's documentation on UINavigationController: This class is not intended for subclassing.

While this intention is not a ban and may not technically get you rejected (using doesn't appear to reference any private APIs) it should impact your design decision. You're likely to inherit unexpected behavior now or potentially in the future (if something changes to the internals of the superclass). Navigation controller's are handy things, but depending on what you need, you might just be better off rolling your own.

One alternative might be to employ a standard navigation controller, but set it's navigationBar property to hidden. You may then design and include your own navigation bar in a XIB with whatever buttons targeted at whatever actions you wish (eg, a back button simply calling popViewController).

EDIT: Subclassing of UINavigationController is no longer discouraged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM