I would like to create my own UIStoryBoard and force the system to use it (inspired by Jody's answer).
How can I do it?
Creating a class @interface MyStoryboard : UIStoryboard
is not being invoked.
Step 1: Set a Storyboard IDIn the Storyboard, select the view controller that you want to instantiate in code. Make sure the yellow circle is highlighted, and click on the Identity Inspector. Set the custom class as well as the field called "Storyboard ID". You can use the class name as the Storyboard ID.
A UIStoryboard object manages archived versions of your app's view controllers. At design time, you configure the content of your view controllers visually, and Xcode saves the data needed to recreate that interface in a storyboard file in your app's bundle.
You were on the right path subclassing UIStoryboard
: all you need to do now is to plug it into your application. The simplest way of doing it is in your application delegate's application:didFinishLaunchingWithOptions:
method:
MyStoryboard.h
@interface MyStoryboard : UIStoryboard
-(id)instantiateViewControllerWithIdentifier:(NSString *)identifier;
@end
MyStoryboard.m
@implementation MyStoryboard
-(id)instantiateViewControllerWithIdentifier:(NSString *)identifier {
NSLog(@"Instantiating: %@", identifier);
return [super instantiateViewControllerWithIdentifier:identifier];
}
@end
MyAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UIStoryboard *storyboard = [MyStoryboard storyboardWithName:@"<identifier-of-your-storyboard>" bundle:nil];
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
With this code in place, you will see calls of NSLog
every time the instantiateViewControllerWithIdentifier:
method is called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With