久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

傳參方法:sharedApplication, NSUserDefaults, protocol 和 delegate(實(shí)例)

 plumbiossom 2014-05-29

1. iOS開發(fā)中使用[[UIApplication sharedApplication] openURL:] 加載其它應(yīng)用

 

在iOS開發(fā)中,,經(jīng)常需要調(diào)用其它App,,如撥打電話、發(fā)送郵件等,。UIApplication:openURL:方法是實(shí)現(xiàn)這一目的的最簡(jiǎn)單方法,該方法一般通過提供的url參數(shù)的模式來調(diào)用不同的App,。

 

通過openURL方法可以調(diào)用如下應(yīng)用:

 

調(diào)用瀏覽器(Safari Browser)

C代碼  收藏代碼
  1. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http:google.com"]];  
 

調(diào)用谷歌地圖(Google Maps)

C代碼  收藏代碼
  1. NSString *addressText = @"7 Hanover Square, New York, NY 10004";  
  2. addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];  
  3. NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];  
  4. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];  

 

調(diào)用郵件客戶端(Apple Mail)

C代碼  收藏代碼
  1. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://[email protected]"]];  
 

撥號(hào)(Phone Number)

C代碼  收藏代碼
  1. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://6463777303"]];  
 

調(diào)用短信(SMS)

C代碼  收藏代碼
  1. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://466453"]];  
 

調(diào)用應(yīng)用商店(AppStore)

C代碼  收藏代碼
  1. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&mt=8"]];  

 

2. NSUserDefaults讀取和寫入自定義對(duì)象

C代碼  收藏代碼
  1. NSString *string = [NSString stringWithString @"data is here"];    
  2. NSUserDefaults *data = [NSUserDefaults standardUserDefaults];    
  3. [data setObject:string forKey:@"key"];    
  4. NSString *value;    
  5. value = [data objectForKey:"key"];    
 

但是并不是所有的東西都能往里放的,。NSUserDefaults只支持: NSString, NSNumber, NSDate, NSArray, NSDictionary.

 

3. protocol 和 delegate 回調(diào)函數(shù)傳值

 

一,、說明
  1.協(xié)議聲明了可以被任何類實(shí)現(xiàn)的方法
  2.協(xié)議不是類,它是定義了一個(gè)其他對(duì)象可以實(shí)現(xiàn)的接口
  3.如果在某個(gè)類中實(shí)現(xiàn)了協(xié)議中的某個(gè)方法,,也就是這個(gè)類實(shí)現(xiàn)了那個(gè)協(xié)議,。
  4.協(xié)議經(jīng)常用來實(shí)現(xiàn)委托對(duì)象。一個(gè)委托對(duì)象是一種用來協(xié)同或者代表其他對(duì)象的特殊對(duì)象,。
  5:委托,,就是調(diào)用自己定義方法,別的類來實(shí)現(xiàn),。
  6.新特性說明
    @optional預(yù)編譯指令:表示可以選擇實(shí)現(xiàn)的方法
    @required預(yù)編譯指令:表示必須強(qiáng)制實(shí)現(xiàn)的方法

 

二,、定義

 

.h

C代碼  收藏代碼
  1. @protocol ContactCtrlDelegate  
  2. - (void)DismissContactsCtrl;  
  3. - (void)CallBack:(NSString *)str; //回調(diào)傳值  
  4. @end  
  5.   
  6. @interface ContactsCtrl : UIViewController {  
  7.     id <ContactCtrlDelegate> delegate;  
  8. }  
  9. @property (nonatomic, assign) id <ContactCtrlDelegate> delegate;  
 

.m

C代碼  收藏代碼
  1. @synthesize delegate;  

 

三、Demo

 

二級(jí)窗口(子窗口)UIViewController subclass

 

  • 1 Textfield
  • 1 Button

1,、ContactsCtrl.h  

C代碼  收藏代碼
  1. #import <UIKit/UIKit.h>  
  2.   
  3. //定義協(xié)議  
  4. @protocol ContactCtrlDelegate  
  5.   
  6. - (void)DismissContactsCtrl;      //回調(diào)關(guān)閉窗口  
  7. - (void)CallBack:(NSString *)str; //回調(diào)傳值  
  8.   
  9. @end  
  10.   
  11.   
  12. @interface ContactsCtrl : UIViewController  
  13. {  
  14.     __weak IBOutlet UITextField *passData; //textfield  
  15.     id <ContactCtrlDelegate> delegate;     //開放delegate  
  16.     NSString *passedVal;                   //從主窗口獲取傳值          
  17. }  
  18.   
  19. @property(nonatomic,retain)id <ContactCtrlDelegate> delegate;  
  20. @property(nonatomic,retain)NSString *passedVal;  
  21.   
  22. - (IBAction)cancelBtn:(id)sender;  
  23.   
  24. @end  
 

2,、ContactsCtrl.m

C代碼  收藏代碼
  1. @implementation ContactsCtrl  
  2. @synthesize delegate;  
  3. @synthesize passedVal;  
  4.   
  5. - (void)viewDidLoad  
  6. {  
  7.     [super viewDidLoad];  
  8.     // Do any additional setup after loading the view from its nib.  
  9.     passData.text = passedVal;  
  10. }  
  11.   
  12. //調(diào)用協(xié)議中的方法  
  13. - (IBAction)cancelBtn:(id)sender  
  14. {  
  15.     [delegate CallBack:[NSString stringWithFormat:@"%@",passData.text]];  
  16.     [delegate DismissContactsCtrl];  
  17. }  
 

一級(jí)窗口(父窗口)

 

  • 1 Textfield
  • 1 Button

3、ViewController.h

C代碼  收藏代碼
  1. #import <UIKit/UIKit.h>  
  2. #import "ContactsCtrl.h"  //引入二級(jí)文件  
  3.   
  4. @interface ViewController : UIViewController <ContactCtrlDelegate>  
  5. {  
  6.     ContactsCtrl *contactsView;  //定義  
  7.     __weak IBOutlet UITextField *textfield;  
  8. }  
  9.   
  10. @property(nonatomic,retain) ContactsCtrl *contactsView;  
  11.   
  12. - (IBAction)addContactsView:(id)sender;  
  13.   
  14. @end  
 

4,、ViewController.m

C代碼  收藏代碼
  1. #import "ViewController.h"  
  2.   
  3. @implementation ViewController  
  4. @synthesize contactsView;  
  5.   
  6. - (IBAction)addContactsView:(id)sender  
  7. {  
  8.     ContactsCtrl *contactView = [[ContactsCtrl alloc] initWithNibName:nil bundle:nil];  
  9.     self.contactsView = contactView;  
  10.     contactsView.delegate = self;  //設(shè)置委托  
  11.     contactsView.passedVal = textfield.text;  
  12.     [self presentModalViewController:contactsView animated:YES];  
  13. }  
  14.   
  15. //實(shí)現(xiàn)ContactCtrlDelegate協(xié)議中的方法  
  16. - (void)DismissContactsCtrl  
  17. {  
  18.     [contactsView dismissModalViewControllerAnimated:YES];  
  19. }  
  20.   
  21. - (void)CallBack:(NSString *)str  
  22. {  
  23.     textfield.text = str;  
  24. }  
 

本實(shí)例是在:http://www./blog-21-13.html  基礎(chǔ)上修改而成,。

 

參考:delegate和protocol

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多