Simple instantiateViewControllerWithIdentifier category
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> | |
@protocol StoryboardInstantiable <NSObject> | |
+ (NSString *)storyboardID; | |
@end | |
@interface UIViewController (StoryboardInstantiable) | |
+ (UIViewController *)instanceByStoryboardID:(NSString *)storyboardID andError:(NSError **)error; | |
+ (UIViewController *)instanceByStoryboardID:(NSString *)storyboardID; | |
@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 "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