Abdelrahman Eid
Abdelrahman Eid

Reputation: 901

Changing "initWithNibName" to "storyboardWithName"

I followed some tutorial to create an open doors animation during the app launch but it's calling an

xib file and I want to call storyboard and I don' have enough experience with this. Here's my code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[OpenDoorsViewController alloc] initWithNibName:@"OpenDoorsViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

Upvotes: 1

Views: 3670

Answers (2)

Gabriele Petronella
Gabriele Petronella

Reputation: 108159

If you simply want to load the initial view controller of the storyboard when the app launches, just return YES in application:didFinishLaunchingWithOptions:.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    return YES;
}

If you want to load a specific controller from the storyboard, you need to first get the storybard instance by

UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];

then use it to instantiate the controller you need

UIViewController * controller = [storyboard instantiateViewControllerWithIdentifier:@"controllerIdentifier"];

where controllerIdentifier has been assigned as storyboard identifier to the controller in Interface Builder.

Here's an example loading a specific view controller, presenting it at launch.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
    UIViewController * controller = [storyboard instantiateViewControllerWithIdentifier:@"controllerIdentifier"];
    self.window.rootViewController = controller;
    return YES;
}

Upvotes: 5

supermarin
supermarin

Reputation: 638

If you start a new iOS project and select 'Use storyboards', the Storyboard will be automatically preloaded for you.

Storyboard is a place with all the controllers (scenes) of your app, and to reference one, you'll need to use

    UIViewController *controller = [[UIStoryboard storyboardWithName:@"storyboard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"an identifier"];

Upvotes: 1

Related Questions