如何创建项目模板 [英] how to create a project template

查看:27
本文介绍了如何创建项目模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

On the subject of custom templates. I am teaching myself how to do this with xcode 7 and objective c and I am stuck. So far, through reading other posts on S.O. I have managed to create a custom template by copying the Single View application one and dropping it in the right directory of xcode packages. My template folder looks like this:

如果我单击Main.storyboard并说添加一些按钮,然后保存它,然后在xcode中打开我的自定义模板,则按钮在那里,所以我知道我正在进步.不过,我想做的是创建视图控制器文件和Xib,并包含应用程序图标等...,以便我可以使用适当自定义的模板来启动所有项目.这就是我被困住的地方.我不确定如何从简单的文件Main.storyboard开始.任何人都可以请我指出正确的汇率.

If I click on Main.storyboard and say add a few buttons, and save it, and then open my custom template in xcode, the buttons are there, so I know I am making progress. What I would like to do though, is create view controller files and xib and include app icons etc... so that I can have a properly customized template to start all my projects with. And this is where I am stuck. I am not sure how to do this starting from the simple file Main.storyboard. Could anyone please just point me in the right direciton.

特别是,我要执行的操作完全没有故事板,而是从xib开始.我猜想我需要对TemplateInfo.plist做一些事情.我看了看又找不到任何有关它的信息,因此只需重定向即可.谢谢

Particularly, what I am looking at doing is not having a storyboard at all and start with an xib. I am guessing I need to do something with the TemplateInfo.plist. I have looked and looked and can't find anything about it, so just a redirection would do. Thanks

推荐答案

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

Creating Xcode templates can be quite complex because most anything that can be edited a project can be scripted. To reiterate what you already know, one should create a new folder in the ~/Library/Developer/Xcode/Templates directory. This is one of the locations Xcode scans on project creation. Naming this folder "Custom Templates" for example will make it show up under that name in Xcode's project creation dialog. In this new folder create another folder named "baseApp.xctemplate". This will make a template named baseApp appear once all our files are in it.

通过复制Xcode现有模板中的一个,最容易完成新模板的创建.通常是因为从头开始编写所有重要的TemplateInfo.plist会很麻烦.这些文件位于Xcode应用程序中.在搜索器中显示其内容,并导航至:/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application

Starting new templates is easiest done by copying one of Xcode's existing ones. Mostly because writing the all-important TemplateInfo.plist from scratch can be cumbersome. These files are located in the Xcode app. Show its contents in the finder and navigate to: /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支持等选项.

From the "Single View Application" template copy all 3 files to the baseApp.xctemplate folder made earlier. This next step is crucial, but after that the first template is all but ready. Double-click the TemplateInfo.plist file to open it in Xcode and change the last part of the Identifier key to baseApp. That's it - the new project dialog in Xcode now should look something like the image below. It would be a good idea to change your TemplateIcon.tiff. Other than that BaseApp is complete with options for Objective-c as well as Swift, Storyboards, CoreData support and more.

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

Now create a new Xcode project named "xibApp" based on the new baseApp template (no options). Then follow these 4 steps:

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

• delete Main.storyboard & ViewController (move to trash)

•删除Info.plist中的主故事板文件库名称"条目

• delete the "Main storyboard file base name" entry in the Info.plist

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

• add a new Cocoa Touch Class called ViewController based on UIViewController and check the box to include a xib file

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

• replace the didFinishLaunchingWithOptions function in AppDelegate with:

    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",如果保留为空白,则会将所有产生的项目变为情节提要项目.为了弥补这一点,我们将创建自己的祖先.

You should now be able to run the app. If everything looks OK quit Xcode. Duplicate the baseApp.xctemplate in your Custom Templates directory and rename it to "xibApp.xctemplate". Then delete the Main.storyboard file in that directory and copy AppDelegate, Info.plist, ViewController.swift and ViewController.xib files from the xibApp folder to the xibApp.xctemplate folder. Double-click the TemplateInfo.plist in the xibApp folder to open it in Xcode and rename the identifier to "com.apple.dt.unit.xibApp". Another thing that needs addressing is the first of the ancestors in this file. It is set to "com.apple.dt.unit.storyboardApplication" and if left would make all resulting projects into storyboard projects. In order to remedy that we are going to create our own ancestor.

Xcode使用祖先层次结构来包含整个项目树.只要扩展名为.xctemplate的文件夹位于应用程序本身或〜/Library/Developer/Xcode/中的Xcode目录中,都可以充当其祖先.此外,这些目录必须包含正确设置的TemplateInfo.plist文件.因此,请抓住Xcode应用程序(开发人员/平台/iPhoneOS.platform/Developer/Library/Xcode/模板/项目模板/iOS/应用程序)内容中"Cocoa Touch Application Base.xctemplate"内的TemplateInfo.plist.将此文件放在自定义模板"文件夹内的新文件夹中,并将文件夹命名为"Custom Xib Base.xctemplate".该文件夹的名称将成为我们祖先的名称.Xcode将删除名称中的所有空格,并将其放入"customXibBase"中,这就是我们新祖先的扩展名:"com.apple.dt.unit.customXibBase".由于Custom Xib Base.xctemplate文件夹可能成为其他模板的祖先,因此最好将其移动到Custom Templates目录(即共享")中一个新的恰当命名的子文件夹.

Xcode uses the ancestor hierarchy to include whole trees of projects. Any folder with the extension .xctemplate can serve as ancestor as long as it is in an Xcode directory - either the app's itself or in ~/Library/Developer/Xcode/. Additionally these directories have to contain a TemplateInfo.plist file that is set up correctly. So grab the TemplateInfo.plist inside "Cocoa Touch Application Base.xctemplate" which is located in the contents of the Xcode app (Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application). Put this file in a new folder inside the "Custom Templates" folder and name the folder "Custom Xib Base.xctemplate". The name of this folder will become the name for our ancestor. Xcode will delete all spaces in the name and make it into "customXibBase" and that's our new ancestor's extension: "com.apple.dt.unit.customXibBase". Since the Custom Xib Base.xctemplate folder may become an ancestor to other templates, it may be a good idea to move it to a new aptly named sub-folder in the Custom Templates directory (i.e. "shared").

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

Back to the TemplateInfo.plist inside the xibApp.xctemplate directory. Double-click it, click the disclosure triangle next to Ancestors and under item one replace "storyboardApplication" with "customXibBase" so that the whole line reads "com.apple.dt.unit.customXibBase". Finally we need to provide Definitions and Nodes for the 4 files we are including as seen in the image below (AppDelegate, Info.plist, ViewController.swift and ViewController.xib). Save the file and quit Xcode. All done. If everything went alright there should now be a new project template under the Custom Templates category called xibApp! I would consider this a workable but hackish approach towards generating new templates. It will probably break with newer versions of Xcode. But it's a good starter to further explore project templates.

xibApp.xctemplate的最终TemplateInfo.plist:

The final TemplateInfo.plist for xibApp.xctemplate:

<?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的模板,创建新的自定义祖先,并允许通过添加新的定义和节点向模板添加所需数量的文件.

Any issues with this answer? Please let me know. This gets you the xib based template, creates a new custom ancestor and allows to add as many files as needed to the template by adding new definitions and nodes.

这篇关于如何创建项目模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆