【问题标题】:how to create a project template如何创建项目模板
【发布时间】:2016-06-06 18:20:33
【问题描述】:

关于自定义模板的主题。 我正在教自己如何使用 xcode 7 和目标 c 来做到这一点,但我被卡住了。到目前为止,通过阅读 S.O. 上的其他帖子。我设法通过复制单一视图应用程序并将其放在 xcode 包的正确目录中来创建自定义模板。我的模板文件夹如下所示:

如果我点击 Main.storyboard 并说添加几个按钮并保存它,然后在 xcode 中打开我的自定义模板,按钮就在那里,所以我知道我正在取得进展。不过,我想做的是创建视图控制器文件和 xib 并包含应用程序图标等......这样我就可以拥有一个正确定制的模板来启动我的所有项目。这就是我卡住的地方。我不确定如何从简单的文件 Main.storyboard 开始执行此操作。谁能给我指出正确的方向。

特别是,我正在考虑做的是根本没有故事板,而是从 xib 开始。我猜我需要对 TemplateInfo.plist 做点什么。我看了又看,找不到任何关于它的东西,所以只需重定向即可。 谢谢

【问题讨论】:

    标签: xcode templates customization


    【解决方案1】:

    创建 Xcode 模板可能相当复杂,因为大多数可以编辑的项目都可以编写脚本。重申一下你已经知道的,应该在 ~/Library/Developer/Xcode/Templates 目录中创建一个新文件夹。这是 Xcode 在项目创建时扫描的位置之一。例如,将此文件夹命名为“自定义模板”将使其在 Xcode 的项目创建对话框中以该名称显示。在这个新文件夹中创建另一个名为“baseApp.xctemplate”的文件夹。一旦我们的所有文件都在其中,这将使名为 baseApp 的模板出现。

    通过复制 Xcode 的现有模板之一来启动新模板是最简单的。主要是因为从头开始编写最重要的 TemplateInfo.plist 可能很麻烦。这些文件位于 Xcode 应用程序中。在查找器中显示其内容并导航到: /Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application

    从“单一视图应用程序”模板将所有 3 个文件复制到之前创建的 baseApp.xctemplate 文件夹。下一步至关重要,但此后第一个模板几乎已准备就绪。双击 TemplateInfo.plist 文件以在 Xcode 中打开它并将 Identifier 键的最后一部分更改为 baseApp。就是这样 - Xcode 中的新项目对话框现在应该如下图所示。更改您的 TemplateIcon.tiff 是个好主意。除此之外,BaseApp 还提供 Objective-c 以及 Swift、Storyboards、CoreData 支持等选项。

    现在基于新的 baseApp 模板创建一个名为“xibApp”的新 Xcode 项目(无选项)。然后按照以下 4 个步骤操作:

    • 删除 Main.storyboard 和 ViewController(移至垃圾箱)

    • 删除 Info.plist 中的“主情节提要文件基本名称”条目

    • 基于 UIViewController 添加一个名为 ViewController 的新 Cocoa Touch 类,并选中该框以包含一个 xib 文件

    • 将 AppDelegate 中的 didFinishLaunchingWithOptions 函数替换为:

        window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
    
        let viewController = ViewController()
    
        window!.rootViewController = viewController
        window!.makeKeyAndVisible()
    
        return true
    

    您现在应该可以运行该应用了。如果一切正常,请退出 Xcode。在自定义模板目录中复制 baseApp.xctemplate 并将其重命名为“xibApp.xctemplate”。然后删除该目录中的 Main.storyboard 文件并将 AppDelegate、Info.plist、ViewController.swift 和 ViewController.xib 文件从 xibApp 文件夹复制到 xibApp.xctemplate 文件夹。双击xibApp文件夹中的TemplateInfo.plist,在Xcode中打开,将标识符重命名为“com.apple.dt.unit.xibApp”。需要解决的另一件事是此文件中的第一个祖先。它设置为“com.apple.dt.unit.storyboardApplication”,如果离开,所有生成的项目都会变成故事板项目。为了解决这个问题,我们将创造我们自己的祖先。

    Xcode 使用祖先层次结构来包含整个项目树。任何扩展名为 .xctemplate 的文件夹都可以作为祖先,只要它位于 Xcode 目录中 - 无论是应用程序本身还是在 ~/Library/Developer/Xcode/ 中。此外,这些目录必须包含正确设置的 TemplateInfo.plist 文件。因此,获取位于 Xcode 应用程序内容(Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application)中的“Cocoa Touch Application Base.xctemplate”中的 TemplateInfo.plist。将此文件放在“自定义模板”文件夹内的新文件夹中,并将文件夹命名为“自定义 Xib Base.xctemplate”。此文件夹的名称将成为我们祖先的名称。 Xcode 将删除名称中的所有空格并将其变为“customXibBase”,这就是我们新祖先的扩展名:“com.apple.dt.unit.customXibBase”。由于 Custom Xib Base.xctemplate 文件夹可能成为其他模板的祖先,因此最好将其移动到 Custom Templates 目录中一个新的恰当命名的子文件夹(即“共享”)。

    回到 xibApp.xctemplate 目录中的 TemplateInfo.plist。双击它,单击 Ancestors 旁边的显示三角形,并在第一项下将“storyboardApplication”替换为“customXibBase”,以便整行显示为“com.apple.dt.unit.customXibBase”。最后,我们需要为我们包含的 4 个文件提供定义和节点,如下图所示(AppDelegate、Info.plist、ViewController.swift 和 ViewController.xib)。保存文件并退出 Xcode。全部做完。 如果一切顺利,现在应该有一个名为 xibApp 的自定义模板类别下的新项目模板!我认为这是生成新模板的可行但笨拙的方法。它可能会与较新版本的 Xcode 中断。但这是进一步探索项目模板的良好开端。

    xibApp.xctemplate 的最终 TemplateInfo.plist:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>Kind</key>
        <string>Xcode.Xcode3.ProjectTemplateUnitKind</string>
        <key>Identifier</key>
        <string>com.apple.dt.unit.xibApp</string>
        <key>Ancestors</key>
        <array>
            <string>com.apple.dt.unit.customXibBase</string>
            <string>com.apple.dt.unit.coreDataCocoaTouchApplication</string>
        </array>
        <key>Concrete</key>
        <true/>
        <key>Description</key>
        <string>This template provides a starting point for an application that uses a single view. It provides a view controller to manage the view, and a storyboard or nib file that contains the view.</string>
        <key>SortOrder</key>
        <integer>1</integer>
        <key>Options</key>
        <array>
            <dict>
                <key>Identifier</key>
                <string>languageChoice</string>
                <key>Units</key>
                <dict>
                    <key>Objective-C</key>
                    <dict>
                        <key>Nodes</key>
                        <array>
                            <string>ViewController.h:comments</string>
                            <string>ViewController.h:imports:importCocoa</string>
                            <string>ViewController.h:interface(___FILEBASENAME___ : UIViewController)</string>
                            <string>ViewController.m:comments</string>
                            <string>ViewController.m:imports:importHeader:ViewController.h</string>
                            <string>ViewController.m:extension</string>
                            <string>ViewController.m:implementation:methods:viewDidLoad(- (void\)viewDidLoad)</string>
                            <string>ViewController.m:implementation:methods:viewDidLoad:super</string>
                            <string>ViewController.m:implementation:methods:didReceiveMemoryWarning(- (void\)didReceiveMemoryWarning)</string>
                            <string>ViewController.m:implementation:methods:didReceiveMemoryWarning:super</string>
                        </array>
                    </dict>
                    <key>Swift</key>
                    <dict>
                        <key>Nodes</key>
                        <array>
                            <string>ViewController.swift:comments</string>
                            <string>ViewController.swift:imports:importCocoa</string>
                            <string>ViewController.swift:implementation(___FILEBASENAME___: UIViewController)</string>
                            <string>ViewController.swift:implementation:methods:viewDidLoad(override func viewDidLoad(\))</string>
                            <string>ViewController.swift:implementation:methods:viewDidLoad:super</string>
                            <string>ViewController.swift:implementation:methods:didReceiveMemoryWarning(override func didReceiveMemoryWarning(\))</string>
                            <string>ViewController.swift:implementation:methods:didReceiveMemoryWarning:super</string>
                        </array>
                    </dict>
                </dict>
            </dict>
        </array>
        <key>Definitions</key>
        <dict>
            <key>AppDelegate.swift</key>
            <dict>
                <key>Path</key>
                <string>AppDelegate.swift</string>
                <key>TargetIndices</key>
                <array>
                    <integer>0</integer>
                </array>
            </dict>
            <key>Info.plist</key>
            <dict>
                <key>Path</key>
                <string>Info.plist</string>
                <key>TargetIndices</key>
                <array>
                    <integer>0</integer>
                </array>
            </dict>
            <key>ViewController.swift</key>
            <dict>
                <key>Path</key>
                <string>ViewController.swift</string>
                <key>TargetIndices</key>
                <array>
                    <integer>0</integer>
                </array>
            </dict>
            <key>ViewController.xib</key>
            <dict>
                <key>Path</key>
                <string>ViewController.xib</string>
                <key>TargetIndices</key>
                <array>
                    <integer>0</integer>
                </array>
            </dict>
        </dict>
        <key>Nodes</key>
        <array>
            <string>AppDelegate.swift</string>
            <string>Info.plist</string>
            <string>ViewController.swift</string>
            <string>ViewController.xib</string>
        </array>
    </dict>
    </plist>
    

    这个答案有什么问题吗?请告诉我。这将为您提供基于 xib 的模板,创建一个新的自定义祖先,并允许通过添加新定义和节点将任意数量的文件添加到模板中。

    【讨论】:

    • 这是一个很好的答案,唯一的问题是没有显示有用的图片。你能把它们包括进来吗?谢谢
    • 当然 - 因为这个答案中已经有很多图片了,如果你更具体地了解你在寻找什么,那将是最好的。
    • 嗨,我一直在尝试这样做,我可以做到这一点,几乎到最后。我唯一的问题是它不包括 ViewController 文件(m 和 n)。我已经尝试将文件添加到文件夹并将它们的名称添加到定义下的 plist 中,对吗?谢谢
    • 您还需要将它们包含在节点下,如上面的示例 plist 中所示。
    • 我确实按照这些思路进行了尝试。我尝试从上面的示例 plist 中获取这一行: ViewController.h:imports:importCocoa 并将其更改为: ViewController.h:imports:#import "ViewController.h"但这没有用。我是否朝着正确的方向前进?我也试过:ViewController.h:imports:"ViewController.h"
    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    相关资源
    最近更新 更多