Skip to content

Instantly share code, notes, and snippets.

@pburleson
Created September 8, 2011 22:45
Show Gist options
  • Save pburleson/1204983 to your computer and use it in GitHub Desktop.
Save pburleson/1204983 to your computer and use it in GitHub Desktop.
pushViewController:animated:willPopHandler:
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment