Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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