Xcode6中如何使用自定义的类模板
说到IOS类的模板,有些人感觉很陌生,但是只要有开发过IOS程序的人,其实都用过类的模板,只不过是用的系统自带的类的模板。
例如创建一个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方法,其实我们无时不刻的使用系统给咱们提供的类模板。
使用类的模板会提高开发的效率,例如在一些大的项目中,我们经常封装一个出一个VC,让其他VC都继承于这个VC 在这个VC中实现基本的方法和逻辑。这样在Xcode中添加自定义的类模板时,其他人继承这个VC的时候,这些基本的方法和逻辑就不用写了。
那如何添加自定义的类模板呢??
举个简单的例子,在一个VC中经常有点击按钮返回上一级页面的操作。要想以后继承这个VC的时候都会有下面新添加的方法
第一步:添加想要的方法和逻辑
- (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方法了。说明添加自定义类模板已成功。。。
测试工程的目录如下:
以上就是自定义模板的全过程。希望对大家有所帮助。
Xcode6中如何使用自定义的类模板的更多相关文章
- YTU 2618: B 求类中数据成员的最大值-类模板
2618: B 求类中数据成员的最大值-类模板 时间限制: 1 Sec 内存限制: 128 MB 提交: 430 解决: 300 题目描述 声明一个类模板,类模板中有三个相同类型的数据成员,有一函 ...
- 2.3在LeetCode中使用我们自定义的类
在上一节中我们使用的是java提供了的类,这一小节中我们就来学习一下如何在LeetCode中使用我们自定义的类. 其实很简单,我们只需把我们编写的自定义类,拷贝到LeetCode提供的类中,形成类中类 ...
- 分布式项目中Spring security自定义权限类
package cn.lijun.core.service; import cn.lijun.core.pojo.seller.Seller;import org.springframework.se ...
- 如何在Web引用中使用项目自定义的类
这个是老架构了,不推荐现在这么用,维护一个老项目记录一下. 项目中WebService和客户端是在一个解决方案下,实体类是一个公用的Project,如果使用Web引用自动生成的类会缺少一些实体类定义的 ...
- Spring Boot中使用Lombok消除POJO类模板代码
首先,要让IDE支持Lombok,这里以idea为例进行介绍. 点击项目的“File”-—>"settings"—>"Plugins",在marke ...
- C++进阶-1-模板基础(函数模板、类模板)
C++进阶 模板 1.1 函数模板 1 #include<iostream> 2 using namespace std; 3 4 // 模板 5 6 // 模板的简单实例 7 // 要求 ...
- C++学习笔记36:类模板
类模板的目的 设计通用的类型式,以适应广泛的成员数据型式 类模板的定义格式 template<模板形式参数列表>class 类名称{...}; 原型:template<typenam ...
- C++:类模板与模板类
6.3 类模板和模板类 所谓类模板,实际上是建立一个通用类,其数据成员.成员函数的返回值类型和形参类型不具体指定,用一个虚拟的类型来代表.使用类模板定义对象时,系统会实参的类型来取代类模板中虚拟类型从 ...
- C++_进阶之函数模板_类模板
C++_进阶之函数模板_类模板 第一部分 前言 c++提供了函数模板(function template.)所谓函数模板,实际上是建立一个通用函数,其函数类型和形参类型不具体制定,用一个虚拟的类型来 ...
随机推荐
- F2工作流引擎Web层全新扁平化UI上线
特点:引入Bootstrap开源UI样式和fontawesome图标集 扁平化样式使用界面更舒服,按钮主题可快速定义更换,对于集成到业主系统UI图标更加丰富. 以下截取部分图片展示,更多请联系作者登录 ...
- span 元素无法设置宽度问题
span 元素为行内元素,没有width属性,需要转换为块级元素才可以设置width: 拓展:html元素分为块级元素,行内元素.可变元素. 行内元素与块级元素直观上的区别 1 .行内元素会在一条直线 ...
- Kettle6.0安装及问题总结-白痴教程
1.安装JDK 配置java环境变量 2.安装KETTLE: 官方下载地址:http://community.pentaho.com/projects/data-integration/ 下载完后,解 ...
- linux 命令行启动虚拟机
vmrun -T ws start /root/vmware/Windows\ Server\ 2012/Windows\ Server\ 2012.vmx nogui
- LeetCode 2 Add Two Numbers 模拟,读题 难度:0
https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-n ...
- Fibonacci(斐波那契)非递归实现。容易看懂
#include<iostream>using namespace std;int main(){ int n; cout<<"please input a n\n& ...
- Codeforces Round #313 (Div. 1)
官方英文题解:http://codeforces.com/blog/entry/19237 Problem A: 题目大意: 给出内角和均为120°的六边形的六条边长(均为正整数),求最多能划分成多少 ...
- (BFS)poj3669-Meteor Shower
题目地址 为判断某时刻能否走到某位置,建立shi数组,记录某位置最早t时刻就不能走.(初始化为-1)之后开始从(0,0)出发bfs,用bu数组记录走到某一位置时花费的步数,并且需要用vi数组记录是否走 ...
- html页面清除缓存
需求:页面每次打开时清除本页面的缓存. 页面打开时,由于缓存的存在,刚刚更新的数据有时无法在页面得到刷新,当这个页面作为模式窗口被打开时问题更为明显. 解决办法为: (1) 用HTML标签设置HTTP ...
- hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏
a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...