diff --git a/Source/UIViewController+RamblerViperModuleTransitionHandlerProtocol.m b/Source/UIViewController+RamblerViperModuleTransitionHandlerProtocol.m index fa1229a..0310c59 100644 --- a/Source/UIViewController+RamblerViperModuleTransitionHandlerProtocol.m +++ b/Source/UIViewController+RamblerViperModuleTransitionHandlerProtocol.m @@ -9,6 +9,7 @@ #import #import "RamblerViperOpenModulePromise.h" #import "RamblerViperModuleFactory.h" +#import "RamblerViperModuleInput.h" static IMP originalPrepareForSegueMethodImp; @@ -100,29 +101,41 @@ + (void)swizzlePrepareForSegue { }); } +static UIViewController *sourceViewControllerFromSegue(UIStoryboardSegue * segue) { + return segue.sourceViewController; +} + +static UIViewController *destinationViewControllerFromSegue(UIStoryboardSegue * segue) { + UIViewController *destinationViewController = segue.destinationViewController; + if ([destinationViewController isKindOfClass:[UINavigationController class]]) { + UINavigationController *navigationController = destinationViewController; + destinationViewController = navigationController.topViewController; + } + return destinationViewController; +} + void RamblerViperPrepareForSegueSender(id self, SEL selector, UIStoryboardSegue * segue, id sender) { ((void(*)(id,SEL,UIStoryboardSegue*,id))originalPrepareForSegueMethodImp)(self,selector,segue,sender); - if (![sender isKindOfClass:[RamblerViperOpenModulePromise class]]) { - return; - } - id moduleInput = nil; - UIViewController *destinationViewController = segue.destinationViewController; - if ([destinationViewController isKindOfClass:[UINavigationController class]]) { - UINavigationController *navigationController = segue.destinationViewController; - destinationViewController = navigationController.topViewController; - } - - id targetModuleTransitionHandler = destinationViewController; + id targetModuleTransitionHandler = destinationViewControllerFromSegue(segue); if ([targetModuleTransitionHandler respondsToSelector:@selector(moduleInput)]) { moduleInput = [targetModuleTransitionHandler moduleInput]; + } else { + return; } - RamblerViperOpenModulePromise *openModulePromise = sender; - openModulePromise.moduleInput = moduleInput; + if ([sender isKindOfClass:[RamblerViperOpenModulePromise class]]) { + RamblerViperOpenModulePromise *openModulePromise = sender; + openModulePromise.moduleInput = moduleInput; + } + + if ([moduleInput respondsToSelector:@selector(setModuleOutput:)]) { + id sourceOutput = [sourceViewControllerFromSegue(segue) moduleInput]; + [moduleInput setModuleOutput:sourceOutput]; + } } @end