Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @interface UINavigationController (Extensions)
- - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated willPopHandler:(void (^)(void))inHandler;
- @end
- #### SNIP HERE ############
- #import "UINavigationController+Extensions.h"
- #import <objc/runtime.h>
- @interface CNavigationControllerExtensionsHelper : NSObject <UINavigationControllerDelegate>
- @property (readwrite, nonatomic, retain) NSMutableDictionary *handlersForControllers;
- - (void)addHandler:(void (^)(void))inHandler forController:(UIViewController *)inViewController;
- @end
- #pragma mark -
- static void *kNavigationControllerExtensionsHelperKey;
- @implementation UINavigationController (Extensions)
- - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated willPopHandler:(void (^)(void))inHandler
- {
- CNavigationControllerExtensionsHelper *theHelper = objc_getAssociatedObject(self, &kNavigationControllerExtensionsHelperKey);
- NSAssert(self.delegate == NULL || self.delegate == theHelper, @"Cannot use on navigation controller that already has a delegate.");
- if (theHelper == NULL)
- {
- theHelper = [[CNavigationControllerExtensionsHelper alloc] init];
- objc_setAssociatedObject(self, &kNavigationControllerExtensionsHelperKey, theHelper, OBJC_ASSOCIATION_RETAIN);
- self.delegate = theHelper;
- }
- [theHelper addHandler:inHandler forController:self.topViewController];
- [self pushViewController:viewController animated:animated];
- }
- @end
- #pragma mark -
- @implementation CNavigationControllerExtensionsHelper
- @synthesize handlersForControllers;
- - (id)init
- {
- if ((self = [super init]) != NULL)
- {
- handlersForControllers = [[NSMutableDictionary alloc] init];
- }
- return self;
- }
- - (void)addHandler:(void (^)(void))inHandler forController:(UIViewController *)inViewController
- {
- NSValue *theKey = [NSValue valueWithPointer:(__bridge void *)inViewController];
- [self.handlersForControllers setObject:[inHandler copy] forKey:theKey];
- }
- - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- {
- NSValue *theKey = [NSValue valueWithPointer:(__bridge void *)viewController];
- void (^theHandler)(void) = [self.handlersForControllers objectForKey:theKey];
- if (theHandler != NULL)
- {
- // NSUInteger theIndex = [navigationController.viewControllers indexOfObject:viewController];
- // NSLog(@"WILL SHOW: %d / %d", theIndex, [navigationController.viewControllers count]);
- theHandler();
- [self.handlersForControllers removeObjectForKey:theKey];
- if (self.handlersForControllers.count == 0)
- {
- objc_setAssociatedObject(self, &kNavigationControllerExtensionsHelperKey, NULL, OBJC_ASSOCIATION_RETAIN);
- navigationController.delegate = NULL;
- }
- }
- }
- @end
Add Comment
Please, Sign In to add comment