如何创建项目模板

时间:2016-06-06 18:20:33

标签: xcode templates customization

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

enter image description here

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

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

1 个答案:

答案 0 :(得分:15)

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

通过复制Xcode的现有模板,可以轻松完成新模板的制作。主要是因为从头开始编写所有重要的TemplateInfo.plist可能很麻烦。这些文件位于Xcode应用程序中。在Finder中显示其内容并导航至: /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支持等选项。

enter image description here

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

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

•删除"主故事板文件基本名称"在Info.plist中输入

•添加一个名为ViewController的新Cocoa Touch类,基于UIViewController并选中该框以包含一个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目录中 - 无论是app本身还是〜/ Library / Developer / Xcode /。此外,这些目录必须包含正确设置的TemplateInfo.plist文件。所以抓住TemplateInfo.plist里面的" Cocoa Touch Application Base.xctemplate"它位于Xcode应用程序的内容中(Developer / Platforms / iPhoneOS.platform / Developer / Library / Xcode / Templates / Project Templates / iOS / Application)。将此文件放在"自定义模板"中的新文件夹中。文件夹并命名文件夹" Custom Xib Base.xctemplate"。该文件夹的名称将成为我们祖先的名称。 Xcode将删除名称中的所有空格并将其转换为" customXibBase"这是我们新祖先的延伸:" com.apple.dt.unit.customXibBase"。由于Custom Xib Base.xctemplate文件夹可能成为其他模板的祖先,因此最好将其移动到自定义模板目录中新的适当命名的子文件夹(即" shared")。

enter image description here

返回xibApp.xctemplate目录中的TemplateInfo.plist。双击它,单击Ancestors旁边的显示三角形,然后在第一项替换" storyboardApplication"使用" customXibBase"所以整行都是" com.apple.dt.unit.customXibBase"。最后,我们需要为我们包含的4个文件提供定义和节点,如下图所示(AppDelegate,Info.plist,ViewController.swift和ViewController.xib)。保存文件并退出Xcode。全部完成。 enter image description here 如果一切顺利,现在应该在自定义模板类别下面有一个名为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的模板,创建新的自定义祖先,并允许通过添加新的定义和节点,根据需要向模板添加任意数量的文件。

相关问题