自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone開發(fā)應用幾個案例實現(xiàn)分析

移動開發(fā) iOS
iPhone開發(fā)應用幾個案例實現(xiàn)分析是本文要介紹的內容,主要是來學習對iphone開發(fā)中幾個小案例的實現(xiàn)進行來講解并分析,具體內容來看本文詳解。

iPhone開發(fā)應用幾個案例實現(xiàn)分析是本文要介紹的內容,主要是來學習對iphone開發(fā)中幾個小案例的實現(xiàn)進行來講解并分析,具體內容來看本文詳解。

1、解析NSString形式xml的代碼 

提出的問題:

  1. NSString *xmlString = @"<person><name>Jack</name><age>13< /age></person>"; 

如何對這個xmlString構造一個NSXML,以及如何解析構造的NSXML.

解決方法:先轉換成NSData,然后用NSXMlParser進行解析。代碼:

  1. - (void)handleXMLData {       
  2.     NSString *myString = @"<addresses owner='swilson'><person><lastName>Doe</lastName><firstName>John</firstName></person></addresses>";   
  3.     NSData *myRequestData = [ NSData dataWithBytes: [myString UTF8String]  length:[myString length]];     
  4.     NSXMLParser *myParser = [[NSXMLParser alloc] initWithData:myRequestData];   
  5.     [myParser setDelegate:self];   
  6.     [myParser setShouldProcessNamespaces:YES];   
  7.     [myParser setShouldReportNamespacePrefixes:YES];   
  8.     [myParser setShouldResolveExternalEntities:NO];   
  9.     BOOL success = [myParser parse];   
  10.     [myParser release];  

2、iphone開發(fā)中讓用戶WebView訪問網(wǎng)頁時嵌入開發(fā)者自己的內容
 
代碼

  1. NSString *strUrl=[textField text];  
  2.   NSString *urlString=[NSString stringWithFormat:strUrl];  
  3.   NSURL *url=[NSURL URLWithString:urlString];  
  4.   NSURLRequest *urlRequest=[NSURLRequest requestWithURL:url  
  5.                                             cachePolicy:NSURLRequestReturnCacheDataElseLoad  
  6.                                         timeoutInterval:60];  
  7.   NSData *urlData;  
  8.   NSURLResponse *response;  
  9.   NSError *error;  
  10.   urlData=[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];  
  11.   NSString *dataStr =[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];  
  12.   dataStr = [dataStr substringToIndex:[dataStr length] - 16];  
  13.   dataStr = [dataStr stringByAppendingString:@"<p>hello world navy did it </p></body></html>"];  
  14.   NSLog(@"%@",dataStr);  
  15.   const char *cString = [dataStr UTF8String];  
  16.   NSData *myData= [[NSData alloc]initWithBytes:cString length:strlen(cString)+1];  
  17.   [self.myWebView loadData:myData MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:url]; 

這個代碼我是在UICatalog中WebViewController中添加的,有人要這功能,小生就乘機學習了下,嫌麻煩,直接用的UICatalog代碼.見諒.

此代碼功能在于:在你訪問的網(wǎng)頁左下角加了hello world navy did it.幾個字.

不希望做流氓功能.一切以用戶為主.

3、iPhone開發(fā)中,動態(tài)調用類和方法

舉一個很簡單的例子:

某公司的有1000名員工, 每個員工的工資都不一樣. 發(fā)工資的時候, 這要是人工去發(fā), 耗費的時間和精力是非常大的. 所以財務會打一個表格給銀行, 委托銀行轉賬.

站在銀行的角度, 如果有1000個公司, 委托銀行轉賬發(fā)工資. 它應該怎么做呢? 它需要通過電子轉賬系統(tǒng), 輸入公司名字, 每個員工的工資數(shù), 就可以實現(xiàn)自動轉賬了.

好, 我們回到 iPhone 開發(fā)上來:

我們現(xiàn)在面臨的情況是, 有10個類, 每個類里頭都有n個方法(前提是方法名有規(guī)律可循,比如 setA0,setA1…) 如果挨個去init類, 然后挨個調用方法,這樣你一天就不用干別的了.

Objective-C里 面,我們可以這樣實現(xiàn):

有數(shù)組: classNames, 存著 類的名字

方法名都是 setA 開頭

  1. for (int c=0; c<[classNames count]; c++) {  
  2. NSString *className=[classNames objectAtIndex:c];  
  3. id class=[[NSClassFromString(className) alloc] init];  
  4. for (int i=0; i<[params count]; i++) {  
  5. [class performSelector:NSSelectorFromString([NSString stringWithFormat:@"setA%i",i])];  
  6. }  

兩個重要的宏 我加大字體標出來了,然后你可以再發(fā)揮一下, 比如傳參數(shù)。

4、iPhone開發(fā)項目中加載本地html文件到uiwebview的代碼

如果您想在iPhone項目中加載Documents里面的文件,可以嘗試CocoaChina版主“lvyile”提供的代碼

  1. - (void)loadDocument:(NSString*)docName {   
  2.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  3.     NSString *documentsDirectory = [paths objectAtIndex:0];      
  4.     NSString *path = [documentsDirectory stringByAppendingPathComponent:docName];      
  5.     NSURL *url = [NSURL fileURLWithPath:path];  
  6.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  7.    
  8.     self.myWebView.scalesPageToFit = YES;  
  9.    
  10.     [self.myWebView loadRequest:request];  

如果加載App內部的文件,需要改一下代碼

  1. NSString   
  2. *mainBundleDirectory = [[NSBundle mainBundle] bundlePath];  
  3. NSString   
  4. *path = [mainBundleDirectory  stringByAppendingPathComponent:docName]; 

小結:iPhone開發(fā)應用幾個案例實現(xiàn)分析的內容介紹完了,希望通過本文的學習能對你有所幫助!更多關于iphone開發(fā)的相關的內容,請參考iphone開發(fā)頻道的內容。

責任編輯:zhaolei 來源: CocoaChina
相關推薦

2011-08-15 15:44:46

iPhone開發(fā)PDF

2011-08-18 16:24:44

iPhone開發(fā)圖片

2011-08-19 11:10:31

iPhone應用

2011-08-16 15:48:37

iPhone開發(fā)抓圖程序

2011-08-18 15:24:40

iPhone國際化

2011-08-19 10:05:30

iPhone開發(fā)

2011-08-17 16:12:20

iPhone應用程序

2011-08-15 18:02:32

iPhone開發(fā)表視圖

2011-08-15 10:06:22

iPhone開發(fā)nib 文件

2011-08-16 15:36:47

iPhone應用測試

2011-08-17 16:23:31

iPhone開發(fā)UIViewContr

2012-05-09 09:49:57

移動支付

2011-07-25 17:07:16

iPhone KVO KVC

2011-08-19 10:01:09

iPhone應用SqliteUITableView

2011-08-18 16:42:07

iPhone應用APNS推送

2011-08-05 13:49:53

iPhone 應用 開發(fā)

2011-08-15 13:50:06

IPhone開發(fā)UIView動畫

2011-07-25 14:44:41

iPhone iPhone開發(fā) 截屏

2011-08-12 11:31:46

iPhoneUIView動畫

2011-08-17 16:29:12

iPhone開發(fā)UIButton
點贊
收藏

51CTO技術棧公眾號