传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)


转自:http://justcoding.iteye.com/blog/1451948


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://devprograms@apple.com"]];  
 

拨号(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. }  

 




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值