iOS開發技巧 -- 複用程式碼片段

來源:互聯網
上載者:User

標籤:

如果你是一位開發人員在開發過程中會發現有些代碼無論是在同一個工程中還是在不同工程中使用率會很高,有經驗的人會直接封裝在一個類裡,或者寫成一個宏定義或者把這些代碼收集起來,下次直接使用,或者放到xcode的代碼片庫裡,直接使用, 從而提高開發效率;

1. 將常用程式碼片段封裝成一個類裡

當一個代碼片在一個或多個工程之中經常出現時,把他封裝在一個類裡面,在使用時候直接傳參即可實現對於功能,或者直接把這類放到另一個工程中同樣使用;

使用UIAlertView舉例

建立一個XF_UIKit類,對於聲明檔案和實現檔案為

 

[cpp] view plaincopy
  1. //  
  2. //  XF_UIKit.h  
  3. //  Demo  
  4. //  
  5. //  Created by DolBy on 13-6-17.  
  6. //  Copyright (c) 2013年 新風作浪. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface XF_UIKit : NSObject  
  12. +(void)showAlert:(NSString *)title withMessage:(NSString *)message witchCancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;  
  13. @end  

 

[cpp] view plaincopy
  1. //  
  2. //  XF_UIKit.m  
  3. //  Demo  
  4. //  
  5. //  Created by DolBy on 13-6-17.  
  6. //  Copyright (c) 2013年 新風作浪. All rights reserved.  
  7. //  
  8.   
  9. #import "XF_UIKit.h"  
  10.   
  11. @implementation XF_UIKit  
  12. +(void)showAlert:(NSString *)title withMessage:(NSString *)message witchCancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION  
  13. {  
  14.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];  
  15.     [alert show];  
  16.     [alert release];  
  17. }  
  18. @end  


使用的時候引入標頭檔直接調用類方法

 

 

[cpp] view plaincopy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     [XF_UIKit showAlert:@"警告" withMessage:@"新風作浪的測試 " witchCancelButtonTitle:@"OK" otherButtonTitles: nil];  
  5.   
  6.     // Do any additional setup after loading the view, typically from a nib.  
  7. }  


 

 

2.使用宏

在 iOS開發中那些高效常用的宏一文例舉了宏的使用,此處不再贅述;

 

3.使用Xcode內建程式碼片段庫

在屬性面板下面有一欄庫面板選擇條,有一項Code Snippet Library有iOS下OS X   和 User(使用者自訂的)程式碼片段儲存

 

(1)在這些庫裡有系統內建的程式碼片段,使用的時候直接拖到工程裡,填上參數即可使用。(2)使用者自訂代碼塊,這也是本文講解的重點。例如:

做過開發的都知道使用表示圖儲存格比較頻繁,經常頻繁寫他們的delegate方法,如果把它們收集起來

 

[cpp] view plaincopy
  1. #pragma mark -  
  2. #pragma mark UITableViewDataSource and UITableViewDelegate Methods  
  3.   
  4. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  5. {  
  6.      
  7.     return 1;  
  8. }  
  9. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  10.       
  11.      
  12.     return 10;  
  13. }  
  14. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  15. {  
  16.     static NSString *CellIdentifier = @"Cell";  
  17.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  18.     if (cell == nil) {  
  19.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  20.     }  
  21.     return cell;  
  22. }  


這是繪製一個基本表示圖單元必須實現的協議方法,全選這些代碼拖到Code Snippet Library裡面,如

 

然後會彈出一個編輯框,然後進行一個簡單編輯,title表示你這個代碼塊標題,Summary 對這段代碼片的簡單介紹,Completion Shortcut表示一個快速鍵,在我們工程中只要輸入快速鍵就可以顯示整段代碼片;

 

相應設定

 

把參數放在兩個#號之間,比如 #參數#

 

編輯完畢選擇Done,在User下即可看到TbaleView代碼塊

 

在使用的時候兩種方式

①直接把這個代碼塊拖到工程中;

②在工程裡面輸入自己設定的快件建代碼,比如剛剛設定的XFTableView,真箇代碼塊全部出現;

 

在資產庫(Libary)/Developer/Xcode/UserData/CodeSnippets下存放的就是你自訂的程式碼片段,如果重裝Xcode記得備份哦!

 

 

原創部落格歡迎轉載分享,請註明出處http://blog.csdn.net/duxinfeng2010

iOS開發技巧 -- 複用程式碼片段

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.