摘要:本文主要向大家介绍了VC编程之Xcode6中如何使用自定义的类模板,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。
本文主要向大家介绍了VC编程之Xcode6中如何使用自定义的类模板,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。
说到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方法了。说明添加自定义类模板已成功。。。
测试工程的目录如下:
以上就是自定义模板的全过程。希望对大家有所帮助。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言VC/MFC频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号