Guest User

Untitled

a guest
Jan 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. @interface UINavigationController (Extensions)
  2.  
  3. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated willPopHandler:(void (^)(void))inHandler;
  4.  
  5. @end
  6.  
  7. #### SNIP HERE ############
  8.  
  9. #import "UINavigationController+Extensions.h"
  10.  
  11. #import <objc/runtime.h>
  12.  
  13. @interface CNavigationControllerExtensionsHelper : NSObject <UINavigationControllerDelegate>
  14. @property (readwrite, nonatomic, retain) NSMutableDictionary *handlersForControllers;
  15. - (void)addHandler:(void (^)(void))inHandler forController:(UIViewController *)inViewController;
  16. @end
  17.  
  18. #pragma mark -
  19.  
  20. static void *kNavigationControllerExtensionsHelperKey;
  21.  
  22. @implementation UINavigationController (Extensions)
  23.  
  24. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated willPopHandler:(void (^)(void))inHandler
  25. {
  26. CNavigationControllerExtensionsHelper *theHelper = objc_getAssociatedObject(self, &kNavigationControllerExtensionsHelperKey);
  27. NSAssert(self.delegate == NULL || self.delegate == theHelper, @"Cannot use on navigation controller that already has a delegate.");
  28. if (theHelper == NULL)
  29. {
  30. theHelper = [[CNavigationControllerExtensionsHelper alloc] init];
  31. objc_setAssociatedObject(self, &kNavigationControllerExtensionsHelperKey, theHelper, OBJC_ASSOCIATION_RETAIN);
  32. self.delegate = theHelper;
  33. }
  34. [theHelper addHandler:inHandler forController:self.topViewController];
  35.  
  36. [self pushViewController:viewController animated:animated];
  37. }
  38.  
  39. @end
  40.  
  41. #pragma mark -
  42.  
  43. @implementation CNavigationControllerExtensionsHelper
  44.  
  45. @synthesize handlersForControllers;
  46.  
  47. - (id)init
  48. {
  49. if ((self = [super init]) != NULL)
  50. {
  51. handlersForControllers = [[NSMutableDictionary alloc] init];
  52. }
  53. return self;
  54. }
  55.  
  56. - (void)addHandler:(void (^)(void))inHandler forController:(UIViewController *)inViewController
  57. {
  58. NSValue *theKey = [NSValue valueWithPointer:(__bridge void *)inViewController];
  59. [self.handlersForControllers setObject:[inHandler copy] forKey:theKey];
  60. }
  61.  
  62. - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
  63. {
  64. NSValue *theKey = [NSValue valueWithPointer:(__bridge void *)viewController];
  65. void (^theHandler)(void) = [self.handlersForControllers objectForKey:theKey];
  66. if (theHandler != NULL)
  67. {
  68. // NSUInteger theIndex = [navigationController.viewControllers indexOfObject:viewController];
  69. // NSLog(@"WILL SHOW: %d / %d", theIndex, [navigationController.viewControllers count]);
  70.  
  71. theHandler();
  72.  
  73. [self.handlersForControllers removeObjectForKey:theKey];
  74.  
  75. if (self.handlersForControllers.count == 0)
  76. {
  77. objc_setAssociatedObject(self, &kNavigationControllerExtensionsHelperKey, NULL, OBJC_ASSOCIATION_RETAIN);
  78. navigationController.delegate = NULL;
  79. }
  80. }
  81. }
  82.  
  83. @end
Add Comment
Please, Sign In to add comment