OSX / macOS - how to migrate a xib to the storyboard

1k Views Asked by At

I have an app with a mainmenu.xib and for various reasons I want to migrate it to a storyboard. Is there a way to do this without creating a new project?

1

There are 1 best solutions below

1
On

This is how to do it:

  1. Create a new project with a storyboard. We're just going to use it to copy one little thing which can't be found in the object library.

  2. Back in your existing project, add a new storyboard ui file (e.g. Main.storyboard) in your existing project. Unlike when starting a new project, the storyboard file will be entirely blank.

  3. From the new project's storyboard, copy the "Application Scene" and paste it into the existing project's storyboard. This is a workaround due to not being able to get an application scene from the object library. After doing this, check that the App Delegate object's "delegate" property there has a referencing outlet to the "Application" by looking in the Connections inspector (right side panel). If it isn't, link them together.

  4. In the object library (right side panel > object library below), add in the window controller. This will add the window controller scene and the view controller scene.

  5. Reference the appropriate classes for the window controller / window / view controller / view if you are using custom classes.

  6. Open your earlier xib file and copy in any views you used before.

  7. In your existing project's xcproj file, go to the info section for the build target and replace the xib dictionary key/value with "NSMainStoryboardFile" for the key and "Main" (or whatever your storyboard file was called sans fileextension) for the value.

Extra notes:

  • If you use custom viewcontrollers and are migrating them to the storyboard, ensure they allow for being init'ed via initWithCoder.

  • If you need to acquire the viewcontroller from the appdelegate, you can don't do it via NSStoryboard's instantiateWithName methods, that will create a new instance. Instead do the following:

    NSApplication *application = [NSApplication sharedApplication];
    
    NSWindow *window = application.windows[0];
    
    self.myCustomViewController = (MyCustomViewController *)window.contentViewController;