Created
May 9, 2011 00:15
-
-
Save threeve/961836 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 <UIKit/UIKit.h> | |
@interface ZMBViewController : UIViewController | |
@end |
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 "ZMBViewController.h" | |
#import <objc/runtime.h> | |
@interface ZMBViewController () | |
@property (retain) NSMutableSet *managedOutlets; | |
@end | |
@implementation ZMBViewController | |
@synthesize managedOutlets; | |
- (void) dealloc; | |
{ | |
// Set all outlets to nil using KVC | |
[self.managedOutlets enumerateObjectsUsingBlock:^(id key, BOOL *stop) { | |
[self setValue:nil forKey:key]; | |
}]; | |
self.managedOutlets = nil; | |
[super dealloc]; | |
} | |
- (void) loadView; | |
{ | |
// Swizzle in our version of setValue:forKey: that tracks KVO | |
Method orig = class_getInstanceMethod([self class], @selector(setValue:forKey:)); | |
Method mine = class_getInstanceMethod([self class], @selector(zmb_setValue:forKey:)); | |
method_exchangeImplementations(orig, mine); | |
self.managedOutlets = [NSMutableSet set]; | |
[super loadView]; | |
// We don't need to track the view outlet, as this will be handled by UIViewController | |
[self.managedOutlets removeObject:@"view"]; | |
// put the original setValue:forKey: back like nothing ever happened. | |
method_exchangeImplementations(orig, mine); | |
} | |
- (void) viewDidUnload; | |
{ | |
[super viewDidUnload]; | |
// Set all outlets to nil using KVC | |
[self.managedOutlets enumerateObjectsUsingBlock:^(id key, BOOL *stop) { | |
[self setValue:nil forKey:key]; | |
}]; | |
self.managedOutlets = nil; | |
} | |
- (void) zmb_setValue:(id)value forKey:(NSString *)key; | |
{ | |
[self.managedOutlets addObject:key]; | |
// this will call the original setValue:forKey | |
// once they are swizzled | |
[self zmb_setValue:value forKey:key]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment