Skip to content

Instantly share code, notes, and snippets.

@alobanov
Last active January 23, 2016 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alobanov/6e81379f2e741c70911a to your computer and use it in GitHub Desktop.
Save alobanov/6e81379f2e741c70911a to your computer and use it in GitHub Desktop.
Simple instantiateViewControllerWithIdentifier category
#import <UIKit/UIKit.h>
@protocol StoryboardInstantiable <NSObject>
+ (NSString *)storyboardID;
@end
@interface UIViewController (StoryboardInstantiable)
+ (UIViewController *)instanceByStoryboardID:(NSString *)storyboardID andError:(NSError **)error;
+ (UIViewController *)instanceByStoryboardID:(NSString *)storyboardID;
@end
#import "UIViewController+StoryboardInstantiable.h"
@implementation UIViewController (StoryboardInstantiable)
+ (UIViewController *)instanceByStoryboardID:(NSString *)storyboardID andError:(NSError **)error {
if (![self conformsToProtocol:@protocol(StoryboardInstantiable)]) {
NSDictionary *d = [NSDictionary dictionaryWithObject:@"Class does not comfort <StoryboardInstantiable> protocol" forKey:NSLocalizedDescriptionKey];
*error = [[NSError alloc] initWithDomain:@"ru.domain" code:-1 userInfo:d];
return nil;
}
UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboardID bundle:nil];
return [sb instantiateViewControllerWithIdentifier:[(id<StoryboardInstantiable>)self storyboardID]];
}
+ (UIViewController *)instanceByStoryboardID:(NSString *)storyboardID {
UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboardID bundle:nil];
return [sb instantiateViewControllerWithIdentifier:[(id<StoryboardInstantiable>)self storyboardID]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment