Last active
August 29, 2015 14:27
-
-
Save k06a/3e8703538d9c4063cf04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Cocoa/Cocoa.h> | |
#import <objc/runtime.h> | |
@implementation NSControl (Subclasses) | |
+ (void)load | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
method_exchangeImplementations(class_getInstanceMethod([self class], @selector(initWithCoder:)), | |
class_getInstanceMethod([self class], @selector(initWithCoder2:))); | |
}); | |
} | |
// Inspired from: http://www.mikeash.com/pyblog/custom-nscells-done-right.html | |
- (id)initWithCoder2:(NSCoder *)coder | |
{ | |
if (![coder isKindOfClass:[NSKeyedUnarchiver class]]) | |
return [self initWithCoder2:coder]; | |
NSKeyedUnarchiver * unarchiver = (NSKeyedUnarchiver *)coder; | |
Class rootclass = [self superclass]; | |
while ([NSStringFromClass(rootclass) rangeOfString:@"NS"].location == NSNotFound) | |
rootclass = [rootclass superclass]; | |
Class supercell = [rootclass cellClass]; | |
Class selfcell = [[self class] cellClass]; | |
if (!selfcell || !supercell) | |
return [self initWithCoder2:coder]; | |
NSString * supercellName = NSStringFromClass(supercell); | |
[unarchiver setClass:selfcell forClassName:supercellName]; | |
self = [self initWithCoder2:coder]; | |
[unarchiver setClass:supercell forClassName:supercellName]; | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment