自定義文件模板
通過自定義一個ViewController的文件模板來自動生成模版代碼
1. Xcode的文件模板路徑(Version9.3)
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates

2. 在File Templates下面新建一個我們自己的文件夾OHCSource
如果要自己創(chuàng)建這些文件模板的話會比較麻煩,所以我們直接從Source文件夾下面,復(fù)制一份Cocoa Touch Class.xctemplate到Custom文件夾中來進(jìn)行修改,將Cocoa Touch Class.xctemplate改成我們自己的名字,例如:OH Touch Class.xctemplate。進(jìn)入OH Touch Class.xctemplate可以看到其中有非常多的文件模板:

因?yàn)橹灰f明ViewController的模板是如何生成的,其他的都是同一個道理,我們只需要修改下面的內(nèi)容:
UIViewControllerObjective-C
UIViewControllerSwift
UIViewControllerXIBObjective-C
UIViewControllerXIBSwift
//模板的圖標(biāo),可以自己用圖標(biāo)進(jìn)行替換
TemplateIcon.png
TemplateIcon@2x.png
//模板配置文件,這個文件是修改的重點(diǎn)
TemplateInfo.plist
3. 把需要的文件夾的名字前綴改成自己的命名
進(jìn)入OHViewControllerObjective-C文件夾,先修改.h文件:
如果出現(xiàn)內(nèi)容無法修改,可將文件復(fù)制到桌面,修改完成后再覆蓋原有文件。
___IMPORTHEADER_cocoaTouchSubclass___
@interface ___FILEBASENAMEASIDENTIFIER___ :
___VARIABLE_cocoaTouchSubclass___
@end
改成:
___IMPORTHEADER_cocoaTouchSubclass___
@interface ___FILEBASENAMEASIDENTIFIER___ : UIViewController
@end
4. 修改.m文件,將.m中的內(nèi)容替換成自定義的代碼模塊:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setupUI];
[self loadData];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Initial Methods
/** 視圖初始化 */
- (void)setupUI {
}
/** 加載數(shù)據(jù) */
- (void)loadData {
}
#pragma mark - Setter & Getter
#pragma mark - Target Mehtods
#pragma mark - Notification Method
#pragma mark - Private Method
#pragma mark - Public Method
#pragma mark - UITableView Delegate &Datasource
#pragma mark - Other Delegate
5. 打開TemplateInfo.plist來修改其中的配置
在Options中有Item0-Item3四個配置,對應(yīng)的是我們創(chuàng)建文件時候的四個選項(xiàng):

修改下面的內(nèi)容:這里的OHViewController要與上面創(chuàng)建的文件夾OHViewControllerObjective-C前面部分一致

6. 修改之后保存
在項(xiàng)目中選擇創(chuàng)建一個新文件,看到最下面多出了自定義的模板,就可以進(jìn)行選擇創(chuàng)建了
