Last active
December 25, 2015 13:49
-
-
Save advantis/6986967 to your computer and use it in GitHub Desktop.
View controller supporting nested storyboard loading
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
// | |
// Copyright © 2013 Yuri Kotov | |
// | |
#import <UIKit/UIKit.h> | |
@interface ADVPlaceholderViewController : 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
// | |
// Copyright © 2013 Yuri Kotov | |
// | |
#import "ADVPlaceholderViewController.h" | |
static void * ObservationContext = &ObservationContext; | |
@implementation ADVPlaceholderViewController | |
#pragma mark - NSCoding | |
- (id) awakeAfterUsingCoder:(NSCoder *)decoder | |
{ | |
__autoreleasing NSString *identifier; | |
__autoreleasing NSString *storyboardName; | |
[self getStoryboard:&storyboardName andIdentifier:&identifier]; | |
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:self.nibBundle]; | |
UIViewController *controller = identifier | |
? [storyboard instantiateViewControllerWithIdentifier:identifier] | |
: [storyboard instantiateInitialViewController]; | |
NSString *keyPath = NSStringFromSelector(@selector(storyboard)); | |
[controller addObserver:self.observer | |
forKeyPath:keyPath | |
options:NSKeyValueObservingOptionOld | |
context:ObservationContext]; | |
return controller; | |
} | |
#pragma mark - NSKeyValueObserving | |
+ (void) observeValueForKeyPath:(NSString *)keyPath | |
ofObject:(id)object | |
change:(NSDictionary *)change | |
context:(void *)context | |
{ | |
if (ObservationContext == context) | |
{ | |
UIStoryboard *storyboard = change[NSKeyValueChangeOldKey]; | |
[object removeObserver:self.observer forKeyPath:keyPath]; | |
[object setValue:storyboard forKey:keyPath]; | |
} | |
} | |
#pragma mark - ADVPlaceholderViewController | |
- (id) observer | |
{ | |
return self.class; | |
} | |
+ (id) observer | |
{ | |
return self; | |
} | |
- (void) getStoryboard:(NSString **)storyboard andIdentifier:(NSString **)identifier | |
{ | |
enum NSUInteger { ComponentStoryboard, ComponentIdentifier }; | |
NSArray *components = [self.title componentsSeparatedByString:@"."]; | |
switch (components.count) | |
{ | |
case 2: | |
*identifier = components[ComponentIdentifier]; | |
case 1: | |
*storyboard = components[ComponentStoryboard]; | |
break; | |
default: | |
NSAssert(NO, @"Invalid 'title' format"); | |
break; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Yuri, while exploring
awakeAfterUsingCoder:
to do similar things with nested storyboards I came across this gist. The KVO for thestoryboard
was the piece I was missing for my implementation. I'd like to leverage that idea in my own open source code and give you credit for the idea. Is that acceptable to you?