Templates with multiple files
Some programming patterns and frameworks require a set of related files, usually with a very specific structure. For example, with the model-view-controller (MVC) pattern, you need separate files for the model, view, and controller.
In AppCode, you can create sets of related files by adding child templates to a file template. When you create a file from such a template, it will also create files from child templates.
Create a template with multiple files
In the Preferences dialog (Ctrl+Alt+S), select .
Create the main file template.
On the Files tab, click and specify the name, file extension, and body of the template.
Select the new template in the list and click on the toolbar. Specify the name, file extension, and body of the child template.
Example: Template for the MVC pattern
Let's say you want to create a table view using the MVC pattern. In this case, you can create the following separate classes:
Model: a data model class.
View: a custom table view cell (a subclass of
UITableViewCell
).Controller: A table view controller that manages the table view logic (a subclass of
UITableViewController
).
In the Preferences dialog (Ctrl+Alt+S), select .
Create the data model class template.
On the Files tab, click and specify the following:
Name:
Table View MVC
Extension:
swift
File name:
${NAME}
Add the following code to the template body:
class ${NAME} { // Data Model }The name of this class will match the name that you provide, for example:
Conference
.Create the view class (
UITableViewCell
) template.Select the new Table View MVC template in the list and click in the toolbar. Specify the following:
File name:
${NAME}Cell
Extension:
swift
Add the following code to the template body:
import UIKit class ${NAME}Cell: UITableViewCell { // Table View Cell }The name of this class will be a combination of the name that you provide and the word
Cell
, for example:ConferenceCell
.Create the controller class template.
Select the Table View MVC template in the list and click in the toolbar. Specify the following:
File name:
${NAME}Controller
Extension:
swift
Add the following code to the template body:
import UIKit class ${NAME}TableViewController: UITableViewController { // Table View Controller }The name of this class will be a combination of the name that you provide and the word
TableViewController
, for example:ConferenceTableViewController
.Click OK to apply the changes.
To use the new template, right-click a directory in the Project tool window or press Alt+Insert and select the Table View MVC template. Specify a name for the model class and AppCode will create all three files.