Skip to content

Commit

Permalink
Merge pull request #9 from vince105/feature/vincepolsinelli/navigatio…
Browse files Browse the repository at this point in the history
…n-swizzling

added will/did pop view controller to swizzling
  • Loading branch information
JamieREvans committed Nov 2, 2015
2 parents f58e99f + 015b969 commit bdfbfbd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Pod/Classes/UIViewController/UIViewController+Navigation.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
@protocol NavigationNotificationExtention <NSObject>
@optional

- (void)viewControllerWasPopped;
- (void)viewWillPop;
- (void)viewDidPop;

@end

@interface UIViewController (Navigation) <NavigationNotificationExtention>

@property (nonatomic, weak) UINavigationController *oldNavigationController;

@end
53 changes: 52 additions & 1 deletion Pod/Classes/UIViewController/UIViewController+Navigation.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,32 @@
//

#import "UIViewController+Navigation.h"
#import "NSObject+Values.h"
#import <FoundationPlus/NSObject+Swizzle.h>
#import <objc/runtime.h>

@interface UIViewController (NavigationPrivate)

@property (nonatomic, weak) UINavigationController *oldNavigationController;

@end

@implementation UIViewController (Navigation)

+ (void)load
{
[self swizzleSelector:@selector(checkIfPopping:) withLocalSelector:@selector(viewWillDisappear:)];
[self swizzleSelector:@selector(checkIfPopped:) withLocalSelector:@selector(viewDidDisappear:)];
}

- (UINavigationController *)oldNavigationController {

return [self valueForSelector:@selector(oldNavigationController)];
}

- (void)setOldNavigationController:(UINavigationController *)oldNavigationController {

objc_setAssociatedObject(self, @selector(oldNavigationController), oldNavigationController, OBJC_ASSOCIATION_ASSIGN);
}

- (void)checkIfPopping:(BOOL)animated
Expand All @@ -22,14 +41,46 @@ - (void)checkIfPopping:(BOOL)animated
if(controllerIndex == NSNotFound)
{
// View is disappearing because it was popped from the stack
if([self respondsToSelector:@selector(viewControllerWasPopped)])[self viewControllerWasPopped];
[self viewWillPop];
}
else
{
// View is disappearing because a new view controller was pushed onto the stack
}

self.oldNavigationController = self.navigationController;

[self checkIfPopping:animated];
}

- (void)checkIfPopped:(BOOL)animated
{
__strong UINavigationController *oldNavigationController = self.oldNavigationController;
if(oldNavigationController)
{
NSUInteger controllerIndex = [oldNavigationController.viewControllers indexOfObject:self];
if(controllerIndex == NSNotFound)
{
// View did disappear because it was popped from the stack
[self viewDidPop];
}
else
{
// View did disappear because a new view controller was pushed onto the stack
}
}

[self checkIfPopped:animated];
}

- (void)viewWillPop
{
// Always implement the method when swizzling and call super in overrides
}

- (void)viewDidPop
{
// Always implement the method when swizzling and call super in overrides
}

@end

0 comments on commit bdfbfbd

Please sign in to comment.