說到IOS類的模板,有些人感受很陌生,可是隻要有開發過IOS程序的人,其實都用過類的模板,只不過是用的系統自帶的類的模板。app
例如建立一個ClassTemplateVC繼承於UIViewController 建立出來的ClassTemplateVC以下:測試
#import "ClassTemplateVC.h" @interface () @end @implementation ClassTemplateVC - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
會有viewDidLoad 和 didReceiveMemoryWarning方法,其實咱們無時不刻的使用系統給我們提供的類模板。spa
使用類的模板會提升開發的效率,例如在一些大的項目中,咱們常常封裝一個出一個VC,讓其餘VC都繼承於這個VC 在這個VC中實現基本的方法和邏輯。這樣在Xcode中添加自定義的類模板時,其餘人繼承這個VC的時候,這些基本的方法和邏輯就不用寫了。3d
那如何添加自定義的類模板呢??code
舉個簡單的例子,在一個VC中常常有點擊按鈕返回上一級頁面的操做。要想之後繼承這個VC的時候都會有下面新添加的方法orm
第一步:添加想要的方法和邏輯blog
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } // 新添加的方法 - (void)backBtnClick:(UIButton *)sender { [self.navigationController popViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
第二步:把.h文件替換成以下的代碼(粘貼複製便可)繼承
// // ___FILENAME___ // ___PROJECTNAME___ // // Created by ___FULLUSERNAME___ on ___DATE___. //___COPYRIGHT___ // ___IMPORTHEADER_cocoaTouchSubclass___ @interface ___FILEBASENAMEASIDENTIFIER___ : ___VARIABLE_cocoaTouchSubclass___ @end
第三步:把對應的.m文件的後替換成以下代碼:圖片
// // ___FILENAME___ // ___PROJECTNAME___ // // Created by ___FULLUSERNAME___ on ___DATE___. //___COPYRIGHT___ // #import "___FILEBASENAME___.h" @interface ___FILEBASENAMEASIDENTIFIER___ () @end @implementation ___FILEBASENAMEASIDENTIFIER___ - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } // 新添加的方法 - (void)backBtnClick:(UIButton *)sender { [self.navigationController popViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
第四步:在桌面上或者其餘地方新建一個文件夾以下圖開發
命名規範爲:類模板的名(ClassTemplateVC)+Objective-C
第五步:把剛纔修改的.h,.m文件複製到第四步建立的文件夾中以下圖
第六步:把.h,.m文件命名爲以下:
注:每一個類模板的文件夾下面都是這樣命名的___FILEBASENAME___.h,___FILEBASENAME___.m(複製粘貼便可)
第七步:找到系統類模板存放的目錄
這裏是Xcode6類模板的路徑:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source
圖片以下:
注:這個路徑是Xcode6中的路徑。之後有可能會變的。
第八步:修改系統的模板配置文件(或者把TemplateInfo.plist文件複製出來修改完成後,替換原有的,這時候須要輸入密碼)
利用上面的路徑找到TemplateInfo.plist文件以下圖
打開這個文件點擊Option->Item 1->Suffixes/Values
1.在Suffixes裏面添加自定義的模板類的類名以及模板類所繼承的類名
2.在Values下面添加自定義模板類的類名
以下圖:
第九步:把第四步建立的文件夾複製到第七步的路徑下(TemplateInfo.plist 同級目錄下)
大功告成。。。。
測試一下新建一個 ClassTemplateTestVC以下:
成功建立後:
ClassTemplateTestVC.m裏面的內容以下:
// // ClassTemplateTestVC.m // ClassTemplate // // Created by StephenLi on 15/7/16. // Copyright (c) 2015年 StephenLi . All rights reserved. // #import "ClassTemplateTestVC.h" @interface ClassTemplateTestVC () @end @implementation ClassTemplateTestVC - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } // 新添加的方法 - (void)backBtnClick:(UIButton *)sender { [self.navigationController popViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
有了剛纔的- (void)backBtnClick:(UIButton *)sender方法了。說明添加自定義類模板已成功。。。
測試工程的目錄以下:
以上就是自定義模板的全過程。但願對你們有所幫助。