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

1. iOS開發中使用[[UIApplication sharedApplication] openURL:] 加載其它應用

 

在iOS開發中,經常需要調用其它App,如撥打電話、發送郵件等。UIApplication:openURL:方法是實現這一目的的最簡單方法,該方法一般通過提供的url參數的模式來調用不同的App。

 

通過openURL方法可以調用如下應用:

 

調用瀏覽器(Safari Browser)

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

調用谷歌地圖(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]];  

 

調用郵件客戶端(Apple Mail)

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

撥號(Phone Number)

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

調用短信(SMS)

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

調用應用商店(AppStore)

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

 

2. NSUserDefaults讀取和寫入自定義對象

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 回調函數傳值

 

一、說明
  1.協議聲明瞭可以被任何類實現的方法
  2.協議不是類,它是定義了一個其他對象可以實現的接口
  3.如果在某個類中實現了協議中的某個方法,也就是這個類實現了那個協議。
  4.協議經常用來實現委託對象。一個委託對象是一種用來協同或者代表其他對象的特殊對象。
  5:委託,就是調用自己定義方法,別的類來實現。
  6.新特性說明
    @optional預編譯指令:表示可以選擇實現的方法
    @required預編譯指令:表示必須強制實現的方法

 

二、定義

 

.h

C代碼  收藏代碼
  1. @protocol ContactCtrlDelegate  
  2. - (void)DismissContactsCtrl;  
  3. - (void)CallBack:(NSString *)str; //回調傳值  
  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

 

二級窗口(子窗口)UIViewController subclass

 

  • 1 Textfield
  • 1 Button

1、ContactsCtrl.h  

C代碼  收藏代碼
  1. #import <UIKit/UIKit.h>  
  2.   
  3. //定義協議  
  4. @protocol ContactCtrlDelegate  
  5.   
  6. - (void)DismissContactsCtrl;      //回調關閉窗口  
  7. - (void)CallBack:(NSString *)str; //回調傳值  
  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. //調用協議中的方法  
  13. - (IBAction)cancelBtn:(id)sender  
  14. {  
  15.     [delegate CallBack:[NSString stringWithFormat:@"%@",passData.text]];  
  16.     [delegate DismissContactsCtrl];  
  17. }  
 

一級窗口(父窗口)

 

  • 1 Textfield
  • 1 Button

3、ViewController.h

C代碼  收藏代碼
  1. #import <UIKit/UIKit.h>  
  2. #import "ContactsCtrl.h"  //引入二級文件  
  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;  //設置委託  
  11.     contactsView.passedVal = textfield.text;  
  12.     [self presentModalViewController:contactsView animated:YES];  
  13. }  
  14.   
  15. //實現ContactCtrlDelegate協議中的方法  
  16. - (void)DismissContactsCtrl  
  17. {  
  18.     [contactsView dismissModalViewControllerAnimated:YES];  
  19. }  
  20.   
  21. - (void)CallBack:(NSString *)str  
  22. {  
  23.     textfield.text = str;  
  24. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章