Skip to content

Instantly share code, notes, and snippets.

@advantis
Last active December 25, 2015 13:49
Show Gist options
  • Save advantis/6986967 to your computer and use it in GitHub Desktop.
Save advantis/6986967 to your computer and use it in GitHub Desktop.
View controller supporting nested storyboard loading
//
// Copyright © 2013 Yuri Kotov
//
#import <UIKit/UIKit.h>
@interface ADVPlaceholderViewController : UIViewController
@end
//
// 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
@levigroker
Copy link

Hi Yuri, while exploring awakeAfterUsingCoder: to do similar things with nested storyboards I came across this gist. The KVO for the storyboard 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment