Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom UIStoryBoard

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.

like image 613
Dejell Avatar asked Dec 25 '12 12:12

Dejell


People also ask

How do you create a storyboard ID?

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.

What is UIStoryboard?

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.


1 Answers

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.

like image 174
Sergey Kalinichenko Avatar answered Nov 03 '22 01:11

Sergey Kalinichenko