標籤:
如果你是一位開發人員在開發過程中會發現有些代碼無論是在同一個工程中還是在不同工程中使用率會很高,有經驗的人會直接封裝在一個類裡,或者寫成一個宏定義或者把這些代碼收集起來,下次直接使用,或者放到xcode的代碼片庫裡,直接使用, 從而提高開發效率;
1. 將常用程式碼片段封裝成一個類裡
當一個代碼片在一個或多個工程之中經常出現時,把他封裝在一個類裡面,在使用時候直接傳參即可實現對於功能,或者直接把這類放到另一個工程中同樣使用;
使用UIAlertView舉例
建立一個XF_UIKit類,對於聲明檔案和實現檔案為
[cpp] view plaincopy
- //
- // XF_UIKit.h
- // Demo
- //
- // Created by DolBy on 13-6-17.
- // Copyright (c) 2013年 新風作浪. All rights reserved.
- //
-
- #import <Foundation/Foundation.h>
-
- @interface XF_UIKit : NSObject
- +(void)showAlert:(NSString *)title withMessage:(NSString *)message witchCancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
- @end
[cpp] view plaincopy
- //
- // XF_UIKit.m
- // Demo
- //
- // Created by DolBy on 13-6-17.
- // Copyright (c) 2013年 新風作浪. All rights reserved.
- //
-
- #import "XF_UIKit.h"
-
- @implementation XF_UIKit
- +(void)showAlert:(NSString *)title withMessage:(NSString *)message witchCancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
- [alert show];
- [alert release];
- }
- @end
使用的時候引入標頭檔直接調用類方法
[cpp] view plaincopy
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [XF_UIKit showAlert:@"警告" withMessage:@"新風作浪的測試 " witchCancelButtonTitle:@"OK" otherButtonTitles: nil];
-
- // Do any additional setup after loading the view, typically from a nib.
- }
2.使用宏
在 iOS開發中那些高效常用的宏一文例舉了宏的使用,此處不再贅述;
3.使用Xcode內建程式碼片段庫
在屬性面板下面有一欄庫面板選擇條,有一項Code Snippet Library有iOS下OS X 和 User(使用者自訂的)程式碼片段儲存
(1)在這些庫裡有系統內建的程式碼片段,使用的時候直接拖到工程裡,填上參數即可使用。(2)使用者自訂代碼塊,這也是本文講解的重點。例如:
做過開發的都知道使用表示圖儲存格比較頻繁,經常頻繁寫他們的delegate方法,如果把它們收集起來
[cpp] view plaincopy
- #pragma mark -
- #pragma mark UITableViewDataSource and UITableViewDelegate Methods
-
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
-
- return 1;
- }
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
-
- return 10;
- }
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
- }
- return cell;
- }
這是繪製一個基本表示圖單元必須實現的協議方法,全選這些代碼拖到Code Snippet Library裡面,如
然後會彈出一個編輯框,然後進行一個簡單編輯,title表示你這個代碼塊標題,Summary 對這段代碼片的簡單介紹,Completion Shortcut表示一個快速鍵,在我們工程中只要輸入快速鍵就可以顯示整段代碼片;
相應設定
把參數放在兩個#號之間,比如 #參數#
編輯完畢選擇Done,在User下即可看到TbaleView代碼塊
在使用的時候兩種方式
①直接把這個代碼塊拖到工程中;
②在工程裡面輸入自己設定的快件建代碼,比如剛剛設定的XFTableView,真箇代碼塊全部出現;
在資產庫(Libary)/Developer/Xcode/UserData/CodeSnippets下存放的就是你自訂的程式碼片段,如果重裝Xcode記得備份哦!
原創部落格歡迎轉載分享,請註明出處http://blog.csdn.net/duxinfeng2010
iOS開發技巧 -- 複用程式碼片段