传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
2012-05-18 09:13
465 查看
总结的不错 直接转了
1. iOS开发中使用[[UIApplication sharedApplication] openURL:] 加载其它应用
在iOS开发中,经常需要调用其它App,如拨打电话、发送邮件等。UIApplication:openURL:方法是实现这一目的的最简单方法,该方法一般通过提供的url参数的模式来调用不同的App。
通过openURL方法可以调用如下应用:
调用浏览器(Safari Browser)
调用谷歌地图(Google Maps)
NSString *addressText = @"7 Hanover Square, New York, NY 10004";
addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];调用邮件客户端(Apple
Mail)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];
拨号(Phone Number)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://6463777303"]];
调用短信(SMS)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&mt=8"]];
2. NSUserDefaults读取和写入自定义对象
3. protocol 和 delegate 回调函数传值
一、说明
1.协议声明了可以被任何类实现的方法
2.协议不是类,它是定义了一个其他对象可以实现的接口
3.如果在某个类中实现了协议中的某个方法,也就是这个类实现了那个协议。
4.协议经常用来实现委托对象。一个委托对象是一种用来协同或者代表其他对象的特殊对象。
5:委托,就是调用自己定义方法,别的类来实现。
6.新特性说明
@optional预编译指令:表示可以选择实现的方法
@required预编译指令:表示必须强制实现的方法
demo
二级窗口(子窗口)UIViewController subclass
1 Textfield
1 Button
1、ContactsCtrl.h
2、ContactsCtrl.m
一级窗口(父窗口)
1 Textfield
1 Button
3、ViewController.h
4、ViewController.m
转自:http://justcoding.iteye.com/blog/1451948
1. iOS开发中使用[[UIApplication sharedApplication] openURL:] 加载其它应用
在iOS开发中,经常需要调用其它App,如拨打电话、发送邮件等。UIApplication:openURL:方法是实现这一目的的最简单方法,该方法一般通过提供的url参数的模式来调用不同的App。
通过openURL方法可以调用如下应用:
调用浏览器(Safari Browser)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http:google.com"]];
调用谷歌地图(Google Maps)
NSString *addressText = @"7 Hanover Square, New York, NY 10004";
addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];调用邮件客户端(Apple
Mail)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];
拨号(Phone Number)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://6463777303"]];
调用短信(SMS)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&mt=8"]];
2. NSUserDefaults读取和写入自定义对象
NSString *string = [NSString stringWithString @"data is here"]; NSUserDefaults *data = [NSUserDefaults standardUserDefaults]; [data setObject:string forKey:@"key"]; NSString *value; value = [data objectForKey:"key"];但是并不是所有的东西都能往里放的。NSUserDefaults只支持: NSString, NSNumber, NSDate, NSArray, NSDictionary.
3. protocol 和 delegate 回调函数传值
一、说明
1.协议声明了可以被任何类实现的方法
2.协议不是类,它是定义了一个其他对象可以实现的接口
3.如果在某个类中实现了协议中的某个方法,也就是这个类实现了那个协议。
4.协议经常用来实现委托对象。一个委托对象是一种用来协同或者代表其他对象的特殊对象。
5:委托,就是调用自己定义方法,别的类来实现。
6.新特性说明
@optional预编译指令:表示可以选择实现的方法
@required预编译指令:表示必须强制实现的方法
demo
二级窗口(子窗口)UIViewController subclass
1 Textfield
1 Button
1、ContactsCtrl.h
#import <UIKit/UIKit.h> //定义协议 @protocol ContactCtrlDelegate - (void)DismissContactsCtrl; //回调关闭窗口 - (void)CallBack:(NSString *)str; //回调传值 @end @interface ContactsCtrl : UIViewController { __weak IBOutlet UITextField *passData; //textfield id <ContactCtrlDelegate> delegate; //开放delegate NSString *passedVal; //从主窗口获取传值 } @property(nonatomic,retain)id <ContactCtrlDelegate> delegate; @property(nonatomic,retain)NSString *passedVal; - (IBAction)cancelBtn:(id)sender; @end
2、ContactsCtrl.m
@implementation ContactsCtrl @synthesize delegate; @synthesize passedVal; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. passData.text = passedVal; } //调用协议中的方法 - (IBAction)cancelBtn:(id)sender { [delegate CallBack:[NSString stringWithFormat:@"%@",passData.text]]; [delegate DismissContactsCtrl]; }
一级窗口(父窗口)
1 Textfield
1 Button
3、ViewController.h
#import <UIKit/UIKit.h> #import "ContactsCtrl.h" //引入二级文件 @interface ViewController : UIViewController <ContactCtrlDelegate> { ContactsCtrl *contactsView; //定义 __weak IBOutlet UITextField *textfield; } @property(nonatomic,retain) ContactsCtrl *contactsView; - (IBAction)addContactsView:(id)sender; @end
4、ViewController.m
#import "ViewController.h" @implementation ViewController @synthesize contactsView; - (IBAction)addContactsView:(id)sender { ContactsCtrl *contactView = [[ContactsCtrl alloc] initWithNibName:nil bundle:nil]; self.contactsView = contactView; contactsView.delegate = self; //设置委托 contactsView.passedVal = textfield.text; [self presentModalViewController:contactsView animated:YES]; } //实现ContactCtrlDelegate协议中的方法 - (void)DismissContactsCtrl { [contactsView dismissModalViewControllerAnimated:YES]; } - (void)CallBack:(NSString *)str { textfield.text = str; }
转自:http://justcoding.iteye.com/blog/1451948
相关文章推荐
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 使用全局变量的四种方式:extern、AppDelegate、定义单例对象、NSUserDefaults 和单例工厂的使用方法
- cocos2d-x打飞机实例总结(一):程序入口分析和AppDelegate,Application,ApplicationProtocol三个类的分析
- iOS: NSKeyedArchiverDelegate Protocol的方法archiver:didEncodeObject:
- iOS: UIScrollViewDelegate Protocol的方法scrollViewDidEndDecelerating:
- iOS: NSUserDefaults 要注意的地方 尤其首次设置默认值的方法
- 总结几种传值--extern,NSUserDefaults,Delegate
- 用系统NSUserDefaults方法保存历史数据
- Android与IOS异同点对比 ( 2 ) ------ 数据存储(SharedPreferences VS NSUserDefaults)
- iOS: UIScrollViewDelegate Protocol的方法scrollViewDidEndScrollingAnimation: