Custom Xcode ViewController class template

Xcode class template.gif

Xcode class templates

For example, creating a UIViewController, a Category, etc., these are actually templates that Xcode helps us integrate.

Custom Xcode class template

When we use the template that comes with Xcode to create a subclass of UIViewController, we will find that the .m file will have some more methods

Default method.jpeg

In our usual development, we have more or less our own specifications, especially in the case of multi-person cooperative development, some good development specifications are quite necessary.

Reference UIViewControllerspecification:

#import "MyViewController.h"

@interface MyViewController ()
//

@end

@implementation MyViewController

#pragma mark - LifeCyle
- (void)viewDidLoad {
    [super viewDidLoad];
}
/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
- (void)dealloc {
    NSLog(@"%@-释放了",self.class);
}

#pragma mark - Intial Methods

#pragma mark - Target Methods

#pragma mark - Private Method

#pragma mark - Setter Getter Methods

#pragma mark - External Delegate

#pragma mark - UITableViewDelegate,UITableViewDataSource
/*
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return <#NSInteger#>;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return <#NSInteger#>;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return <#UITableViewCell#>;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return <#CGFloat#>;
}
**/
@end

Create your own Xcode class template

The template structure looks like this:

Template structure.jpeg

Introduce what's inside (xxx is the name of your custom template):

  • xxx.xctemplatetemplate file.

  • xxxObjective-COC template file, which contains ___FILEBASENAME___.hand___FILEBASENAME___.m

  • xxxXIBObjective-CMore than xxxObjective-Cone ___FILEBASENAME___.xibfile, can be customized.

  • xxxSwiftSwift template file, which contains a ___FILEBASENAME___.swiftfile.

  • xxxXIBSwiftMore than xxxSwiftone ___FILEBASENAME___.xibfile, can be customized.

  • TemplateIcon.pngThe image displayed by the template.

  • TemplateInfo.plistTemplate configuration information.


  • Copy Cocoa Class.xctemplate, rename and delete unnecessary files.

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source

  • Modify .h to
//  ___FILENAME___
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  ___COPYRIGHT___
//

___IMPORTHEADER_cocoaSubclass___

@interface ___FILEBASENAMEASIDENTIFIER___ : UIViewController

@end
  • To modify .m, you only need to add the specifications you need, you can refer to the VCtemplate.
  1. DATEIdentifies the current time;

  2. FILENAMEfull name with file suffix;

  3. FILEBASENAMEThe name without the file suffix;

  4. FULLUSERNAMEthe current username;

  5. PROJECTNAMEproject name;

  6. FILEBASENAMEASIDENTIFIER``VCclass name;

  7. IMPORTHEADER_cocoaSubclassImported header files.

  • Modified .swiftto:
//  ___FILENAME___
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  ___COPYRIGHT___
//

import UIKit

class ___FILEBASENAMEASIDENTIFIER___: UIViewController {

// MARK: - LifeCyle

override func viewDidLoad() {
    super.viewDidLoad()
}

deinit {
    print("\(object_getClassName(self)) - 释放了!")
}

// MARK: - Intial Methods

// MARK: - Target Methods

// MARK: - Private Method

// MARK: - Setter Getter Methods

// MARK: - External Delegate

}
  • New .xib, rename ___FILEBASENAME___.xiband modify as shown below:

xib rename .jpeg

  • TemplateInfo.plist

TemplateInfo.jpeg

SortOrderThe location of the template in the interface;

OptionsCorresponding to the above image selection;

FallbackHeader``.himported header files;

RequiredOptions-> cocoaSubclassWhether to support the selection of xib``Defaultdefault trueautomatic check;

ValuesThe name of the custom template (must be consistent);

SuffixesThe default class name of the template, the effect is as shown below.

Suffixes.gif

Finally, copy the created xxx.xctemplatefile directly to the system path and use it directly

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source

Modify system template

After the View is created, the xib file is automatically created

  • Enter the directory and find the Cocoa Touch Class.xctemplate file

    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source

  • Copy a UIViewObjective-C file and rename it to UIViewXIBObjective-C, then go to another folder with xib, copy a copy of the xib to UIViewXIBObjective-C, and the xib can be modified by yourself. (I only configured Objective-C here, Swift steps are similar)

  • Find the TemplateInfo.plistfile in the current directory and modify it as follows:

edit_TemplateInfo.jpeg

  • ViewYou can choose xibto recreate the system template

Demo

Reference link

Custom Xcode ViewController class template

Xcode7 create custom template

Creating Custom Xcode 4 File Templates

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325549659&siteId=291194637