Skip to content

Instantly share code, notes, and snippets.

@PsychoH13
Last active December 18, 2015 05:59
Show Gist options
  • Save PsychoH13/5737258 to your computer and use it in GitHub Desktop.
Save PsychoH13/5737258 to your computer and use it in GitHub Desktop.
UIStoryboard delegate additions
/*
UIStoryboard+PSYStoryboardDelegate.h
Created by Remy "Psy" Demarest on 08/06/2013.
Copyright (c) 2013. Remy "Psy" Demarest
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import <UIKit/UIKit.h>
@protocol PSYStoryboardDelegate <NSObject>
@optional
- (void)storyboard:(UIStoryboard *)storyboard didInstantiateInitialViewController:(id)initialViewController;
- (void)storyboard:(UIStoryboard *)storyboard didInstantiateViewController:(id)viewController withIdentifier:(NSString *)identifier;
- (void)storyboard:(UIStoryboard *)storyboard willPerformSegue:(UIStoryboardSegue *)segue;
@end
@interface UIStoryboard (PSYStoryboardDelegate)
@property (nonatomic, assign) IBOutlet id<PSYStoryboardDelegate> delegate;
@end
/*
UIStoryboard+PSYStoryboardDelegate.m
Created by Remy "Psy" Demarest on 08/06/2013.
Copyright (c) 2013. Remy "Psy" Demarest
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import "UIStoryboard+PSYStoryboardDelegate.h"
#import <objc/runtime.h>
@implementation UIStoryboard (PSYStoryboardDelegate)
static IMP _replaceMethodImplementation(Class cls, SEL selector, IMP replacement)
{
Method m = class_getInstanceMethod(cls, selector);
return class_replaceMethod(cls, selector, replacement, method_getTypeEncoding(m));
}
static UIStoryboard *(*_old_UIStoryboard_storyboardWithName_bundle_)(Class self, SEL _cmd, NSString *name, NSBundle *bundle);
static UIStoryboard *_UIStoryboard_storyboardWithName_bundle_(Class self, SEL _cmd, NSString *name, NSBundle *bundle)
{
UIStoryboard *storyboard = _old_UIStoryboard_storyboardWithName_bundle_(self, _cmd, name, bundle);
id<PSYStoryboardDelegate> delegate = (id<PSYStoryboardDelegate>)[[UIApplication sharedApplication] delegate];
if (![delegate conformsToProtocol:@protocol(PSYStoryboardDelegate)])
return storyboard;
if(bundle != nil && bundle != [NSBundle mainBundle])
return storyboard;
if (![[bundle objectForInfoDictionaryKey:@"UIMainStoryboardFile"] isEqualToString:name]
&& ![[bundle objectForInfoDictionaryKey:UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? @"UIMainStoryboardFile~ipad" : @"UIMainStoryboardFile~iphone"] isEqualToString:name])
return storyboard;
[storyboard setDelegate:delegate];
return storyboard;
}
+ (void)load
{
_old_UIStoryboard_storyboardWithName_bundle_ = (UIStoryboard *(*)(Class, SEL, NSString *, NSBundle *))_replaceMethodImplementation(object_getClass(self), @selector(storyboardWithName:bundle:), (IMP)_UIStoryboard_storyboardWithName_bundle_);
}
static id (*_old_UIStoryboard_instantiateInitialViewController)(UIStoryboard *self, SEL _cmd);
static id _UIStoryboard_instantiateInitialViewController(UIStoryboard *self, SEL _cmd)
{
id ret = _old_UIStoryboard_instantiateInitialViewController(self, _cmd);
if ([[self delegate] respondsToSelector:@selector(storyboard:didInstantiateInitialViewController:)])
[[self delegate] storyboard:self didInstantiateInitialViewController:ret];
return ret;
}
static id (*_old_UIStoryboard_instantiateViewControllerWithIdentifier_)(UIStoryboard *self, SEL _cmd, NSString *identifier);
static id _UIStoryboard_instantiateViewControllerWithIdentifier_(UIStoryboard *self, SEL _cmd, NSString *identifier)
{
id ret = _old_UIStoryboard_instantiateViewControllerWithIdentifier_(self, _cmd, identifier);
if ([[self delegate] respondsToSelector:@selector(storyboard:didInstantiateViewController:withIdentifier:)])
[[self delegate] storyboard:self didInstantiateViewController:ret withIdentifier:identifier];
return ret;
}
static const void *const _PSYStoryboardSegueIsCallingDelegateKey = &_PSYStoryboardSegueIsCallingDelegateKey;
static BOOL _UIStoryboardSegue_shouldCallDelegate(UIStoryboardSegue *self)
{
return objc_getAssociatedObject(self, _PSYStoryboardSegueIsCallingDelegateKey) == nil;
}
static void _UIStoryboardSegue_setIsCallingDelegate(UIStoryboardSegue *self, BOOL isCalling)
{
objc_setAssociatedObject(self, _PSYStoryboardSegueIsCallingDelegateKey, isCalling ? (id)kCFBooleanTrue : nil, OBJC_ASSOCIATION_ASSIGN);
}
static void _overrideUIStoryboardSeguePerformMethods(void)
{
const Class NSObjectMetaClass = object_getClass([NSObject class]);
const Class UIStoryboardSegueClass = [UIStoryboardSegue class];
const SEL selector = @selector(perform);
unsigned classCount = 0;
Class *allClasses = objc_copyClassList(&classCount);
for(unsigned i = 0; i < classCount; i++)
{
Class currentClass = allClasses[i];
if (object_getClass(object_getClass(currentClass)) != NSObjectMetaClass)
continue;
if(![currentClass isSubclassOfClass:UIStoryboardSegueClass])
continue;
Method performMethod = class_getInstanceMethod(currentClass, selector);
void (*old_UIStoryboardSegue_perform)(UIStoryboardSegue *self, SEL _cmd) = (void (*)(UIStoryboardSegue *self, SEL _cmd))method_getImplementation(performMethod);
IMP new_UIStoryboardSegue_perform = imp_implementationWithBlock(^(UIStoryboardSegue *self) {
if (_UIStoryboardSegue_shouldCallDelegate(self))
{
UIStoryboard *sourceStoryboard = [[self sourceViewController] storyboard];
UIStoryboard *destinationStoryboard = [[self destinationViewController] storyboard];
if([[sourceStoryboard delegate] respondsToSelector:@selector(storyboard:willPerformSegue:)])
[[sourceStoryboard delegate] storyboard:sourceStoryboard willPerformSegue:self];
if(destinationStoryboard != sourceStoryboard && [[destinationStoryboard delegate] respondsToSelector:@selector(storyboard:willPerformSegue:)])
[[destinationStoryboard delegate] storyboard:destinationStoryboard willPerformSegue:self];
}
_UIStoryboardSegue_setIsCallingDelegate(self, YES);
old_UIStoryboardSegue_perform(self, selector);
_UIStoryboardSegue_setIsCallingDelegate(self, NO);
});
method_setImplementation(performMethod, new_UIStoryboardSegue_perform);
}
free(allClasses);
}
static const void *const _PSYStoryboardDelegateKey = &_PSYStoryboardDelegateKey;
- (id<PSYStoryboardDelegate>)delegate
{
return objc_getAssociatedObject(self, _PSYStoryboardDelegateKey);
}
- (void)setDelegate:(id<PSYStoryboardDelegate>)delegate
{
if(delegate != nil)
{
if([delegate respondsToSelector:@selector(storyboard:didInstantiateInitialViewController:)])
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_old_UIStoryboard_instantiateInitialViewController = (id (*)(UIStoryboard *, SEL))_replaceMethodImplementation([UIStoryboard class], @selector(instantiateInitialViewController), (IMP)_UIStoryboard_instantiateInitialViewController);
});
}
if([delegate respondsToSelector:@selector(storyboard:didInstantiateViewController:withIdentifier:)])
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_old_UIStoryboard_instantiateViewControllerWithIdentifier_ = (id (*)(UIStoryboard *, SEL, NSString *))_replaceMethodImplementation([UIStoryboard class], @selector(instantiateViewControllerWithIdentifier:), (IMP)_UIStoryboard_instantiateViewControllerWithIdentifier_);
});
}
if([delegate respondsToSelector:@selector(storyboard:willPerformSegue:)])
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_overrideUIStoryboardSeguePerformMethods();
});
}
}
objc_setAssociatedObject(self, _PSYStoryboardDelegateKey, delegate, OBJC_ASSOCIATION_ASSIGN);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment