说到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方法了。说明添加自定义类模板已成功。。。
测试工程的目录以下:
以上就是自定义模板的全过程。但愿对你们有所帮助。