如何创建项目模板

2023-12-04

关于自定义模板的主题。 我正在自学如何使用 xcode 7 和 Objective C 来做到这一点,但我陷入了困境。到目前为止,通过阅读 S.O. 上的其他帖子我通过复制单视图应用程序并将其放入 xcode 包的正确目录中,成功创建了一个自定义模板。我的模板文件夹如下所示:

enter image description here

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

特别是,我正在做的事情是根本没有故事板并从 xib 开始。我猜我需要对 TemplateInfo.plist 做一些事情。我看了又看,找不到任何相关信息,所以只需重定向就可以了。 谢谢


创建 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 支持等选项。

enter image description here

现在,基于新的 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 文件。因此,请获取“Cocoa Touch Application Base.xctemplate”内的 TemplateInfo.plist,该模板位于 Xcode 应用程序的内容中 (Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application)。将此文件放入“Custom Templates”文件夹内的新文件夹中,并将该文件夹命名为“Custom Xib Base.xctemplate”。该文件夹的名称将成为我们祖先的名称。 Xcode 将删除名称中的所有空格并将其变成“customXibBase”,这就是我们新祖先的扩展名:“com.apple.dt.unit.customXibBase”。由于 Custom Xib Base.xctemplate 文件夹可能成为其他模板的祖先,因此最好将其移动到 Custom Templates 目录中一个适当命名的新子文件夹(即“共享”)。

enter image description here

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. enter image description here 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:

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何创建项目模板 的相关文章

随机推荐

  • 如何将geom_bar、geom_point和geom_line组合成一张图?

    我试图通过三个并行条件 CET RES END 和对干预的响应 高或低 即 CET Hi CET Lo 等 来可视化两个时间点 干预前和干预后 的连续数据点 我想创建一个条形图 显示 X 轴上每个条件的平均输出 并带有单独的时间条 前和后
  • jQuery - 获取与所选输入的 id 具有相同名称的下一个元素

    首先 我想为这个可怕的标题道歉 但这是我能想到的总结我的问题的最好方式 目前 我使用jQuery检查的脚本blur on an input text 输入的文本值大于一定数量 如果没有 输入会突出显示红色 但我接下来想要做的是有一个 p 在
  • 为什么 SendInput() 不起作用?

    我想使用 SendInput 函数自动按下一些键 My code include stdafx h include
  • 如何解决“类型错误:“example”() 不带参数”错误?

    我根据我在 YouTube 上学到的有关类和对象的教程创建了一个简单的 MCQ 并将不同的 python 程序导入其中 以下是我创建的内容 这是我创建的类 class MCQ def int self prompt answer self
  • 如何使用javascript通过onclick隐藏div?

    我使用了 javascript 通过 onclick 显示 div 但是当我单击 div 外部时我想隐藏 div 如何在 JavaScript 中做到这一点 我正在使用 javascript 代码 a href function toggl
  • 应用程序的 Facebook 登录配置错误:Kindle Fire 集成问题

    我们在 Kindle Fire Android 应用程序上集成了 Facebook 登录 大多数时候它都可以正常工作 但有时对于某些用户来说 当他们尝试使用 facebook 登录进行注册时 会失败并显示错误 APp 的 facebook
  • C# 中的动态 Where 子句 lambda

    我有一个如下所示的搜索表单 表单背后的代码如下所示 using Html BeginForm Html ValidationSummary div Html DropDownList SelectedType Model TypeOptio
  • 在 fb_var_screeninfo 中设置 yres_virtual 时出现无效参数错误

    我正在尝试为 Linux 创建一个直接写入帧缓冲区 dev fb0 的应用程序 为了使其成为双缓冲 我尝试使虚拟屏幕成为屏幕大小的两倍 这是我写的程序 struct fb var screeninfo screeninfo var stru
  • nhibernate 审核更新事件

    以下代码适用于插入但适用于更新modifier从未设置过 有什么想法吗 预更新代码正在运行 并将状态和实体值正确设置为所需值 但是 当查看生成的 sql 时 nhibernate 不会在更新查询中包含该字段
  • 我无法使用 iTextSharp 将“Page X of Y”等内容插入到我的 PDF 页脚中

    我是 iTextSharp 的新手 遇到以下情况 我正在创建一个包含页眉和页脚的 PDF 对于页眉和页脚的创建 我正在使用扩展的类 PdfPageEventHelper我已经覆盖了OnStartPage 和OnEndPage 方法 效果很好
  • 如何使用 PHP、MySql 借助 json 验证 Android 中的用户登录凭据

    我是安卓开发新手 我想使用 php mysql 和 json 进行登录验证 我只负责 PHP MySql 和 json 部分 如果用户在android应用程序中输入用户名和密码 那么它需要使用PHP和Mysql检查用户表 并且只需要使用js
  • MySQL根据最新时间戳选择记录组

    我有一个每隔几个小时运行一次的例程 它在用于记录的表中创建多个条目 我需要做的是选择所有最新的记录时间戳具有共同的帐户 ID 像这样的东西 SELECT FROM TABLE logs WHERE ACCOUNT ID 12345 ORDE
  • MYSQL/PHP 查找与给定项目关联的最常见项目

    我有数千个用户生成的物品愿望清单 桌子是这样的 collectionId itemdId user id 123 2345 1 123 3465 1 123 876 1 lt 123 567 1 123 980 1 lt 777 980 2
  • TypeScript 错误:重复的标识符“LibraryManagedAttributes”

    编译失败 moonholdings io node modules types react dom node modules types react index d ts 2312 14 重复的标识符 LibraryManagedAttri
  • 安装 mediapipe 库后 cv2 不起作用

    我想使用 python 制作 handtracker 程序 但教程告诉我安装 mediapipe 库 然后我安装了它 之前使用cv2传输我的相机是可以的 但是安装mediapipe之后 cv2不起作用 这里是消息 gt gt gt impo
  • 对已经排序的数组进行快速排序

    在这个问题中 https www quora com What is randomized quicksort 阿莱霍 豪斯纳 Alejo Hausner 说道 最坏情况下快速排序的成本 that 讽刺的是 如果您将快速排序应用于已经排序的
  • 自动生成 .NET 故障转储

    我知道如何使用 ADPlus 或 DebugDiag 生成故障转储文件 但我想知道是否有一种方法可以在客户的计算机上执行此操作而无需安装这些工具 具体来说 我希望能够配置我的应用程序 例如 使用注册表值 在发生严重故障时生成故障转储 更具体
  • 如何删除字符串的一部分?

    假设我有test 23我想删除test 我怎么做 前面的前缀 可以换 我最喜欢的方法是 拆分和弹出 var str test 23 alert str split pop gt 23 var str2 adifferenttest 153
  • 在 Eclipse Android 中导入 JAR (JAudioTagger)

    我正在开发一个加载 mp3 文件的歌曲数据的程序 我正在尝试导入 JAudioTagger 来帮助加载歌曲信息 JAudioTagger 是一个 jar 文件 我进入导入窗口 但面临许多我不确定的导入选项 看起来有三种可行的选择 EJB J
  • 如何创建项目模板

    关于自定义模板的主题 我正在自学如何使用 xcode 7 和 Objective C 来做到这一点 但我陷入了困境 到目前为止 通过阅读 S O 上的其他帖子我通过复制单视图应用程序并将其放入 xcode 包的正确目录中 成功创建了一个自定