简体   繁体   English

OSX / macOS - 如何将xib迁移到故事板

[英]OSX / macOS - how to migrate a xib to the storyboard

I have an app with a mainmenu.xib and for various reasons I want to migrate it to a storyboard. 我有一个带有mainmenu.xib的应用程序,由于各种原因我想将它迁移到故事板。 Is there a way to do this without creating a new project? 有没有办法在不创建新项目的情况下执行此操作?

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 (eg Main.storyboard) in your existing project. 回到现有项目中,在现有项目中添加一个新的storyboard ui文件(例如Main.storyboard)。 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). 执行此操作后,通过查看Connections检查器(右侧面板),检查App Delegate对象的“委托”属性是否具有“应用程序”的引用插座。 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. 打开早期的xib文件并复制之前使用的任何视图。

  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. 在现有项目的xcproj文件中,转到构建目标的info部分,并使用“ NSMainStoryboardFile ”替换xib字典键/值, NSMainStoryboardFile值替换为“Main”(或任何故事板文件称为sans fileextension)。

Extra notes: 额外说明:

  • If you use custom viewcontrollers and are migrating them to the storyboard, ensure they allow for being init'ed via initWithCoder. 如果您使用自定义视图控制器并将它们迁移到故事板,请确保它们允许通过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. 如果您需要从appdelegate获取viewcontroller,则不能通过NSStoryboard的instantiateWithName方法执行此操作,这将创建一个新实例。 Instead do the following: 而是执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM