8

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.

Community
  • 1
  • 1
Dejell
  • 13,947
  • 40
  • 146
  • 229

1 Answers1

10

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523