本节主要来讲解如何使用委托delegate在不同窗口之间传递数据,具体内容来看下面的详细内容。
比如: 在窗口1中打开窗口2,然后在窗口2中填入一个数字,这个数字又回传给窗口1。
窗口1
窗口2
窗口2的结果传递给窗口1
1、首先定义个一委托UIViewPassValueDelegate用来传递值
@PRotocol UIViewPassValueDelegate - (void)passValue:(NSString *)value; @end
这个protocol 就是用来传递值
2、在窗口1的头文件里,声明delegate
#import <UIKit/UIKit.h> #import "UIViewPassValueDelegate.h" @interface DelegateSampleViewController : UIViewController <UIViewPassValueDelegate> { UITextField *_value; } @property(nonatomic, retain) IBOutlet UITextField *value; - (IBAction)buttonClick:(id)sender; @end
并实现这个委托
- (void)passValue:(NSString *)value { self.value.text = value; NSLog(@"the get value is %@", value); }
button的Click方法,打开窗口2,并将窗口2的delegate实现方法指向窗口1。
- (IBAction)buttonClick:(id)sender { ValueInputView *valueView = [[ValueInputView alloc] initWithNibName:@"ValueInputView" bundle:[NSBundle mainBundle]]; valueView.delegate = self; [self setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; [self presentModalViewController:valueView animated:YES]; }
第二个窗口的实现
.h 头文件
#import <UIKit/UIKit.h> #import "UIViewPassValueDelegate.h" @interface ValueInputView : UIViewController { NSObject<UIViewPassValueDelegate> * delegate; UITextField *_value; } @property(nonatomic, retain)IBOutlet UITextField *value; @property(nonatomic, retain) NSObject<UIViewPassValueDelegate> * delegate; - (IBAction)buttonClick:(id)sender; @end
.m实现文件
#import "ValueInputView.h" @implementation ValueInputView @synthesize delegate; @synthesize value = _value; - (void)dealloc { [self.value release]; [super dealloc]; } - (IBAction)buttonClick:(id)sender { [delegate passValue:self.value.text]; NSLog(@"self.value.text is%@", self.value.text); [self dismissModalViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc. that aren't in use. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } @end
首先写一个单例类,继承NSObject
check.h文件中
@property(strong ,nonatomic) UITable * Table; @property(strong ,nonitomic) UITextFiled * Text; +(check*)shareDataModle;
check.m中
//定义一个静态的checke类的对象,并赋给一个空值
static check * dataModle = nil; +(check*)shareDataModle { if (dataModle == nil) { dataModle = [[check alloc]init]; } }
//在数据源将数据赋值给单例的对象
-(void)checkDataSource{ [check shareDatamodle].Lable = @"15"; [check shareDatamodle].Text = @"22";}
//引入单例的头文件 ,在对应定的方法中给对应的对象赋值
//将单例中的属性值传给当前界面中的接收对象,到此就完成了数据的传送和接收
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.numberLable.text=[check shareDataModle].Lable; self.danHao.text = [check shareDataModle].Text; }
三.iOS开发中使用[[UIapplication sharedApplication] openURL:] 加载其它应用
在iOS开发中,经常需要调用其它App,如拨打电话、发送邮件等。UIApplication:openURL:方法是实现这一目的的最简单方法,该方法一般通过提供的url参数的模式来调用不同的App。
通过openURL方法可以调用如下应用:
调用浏览器(Safari Browser)
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:@"sms://466453"]];
调用应用商店(AppStore)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&amp;mt=8"]];
NSUserDefaults *mySettingData = [NSUserDefaults standardUserDefaults];
创建NSUserDefaults对象之后即可往里面添加数据,它支持的数据类型有NSString、 NSNumber、NSDate、 NSArray、NSDictionary、BOOL、NSInteger、NSFloat等系统定义的数据类型,如果要存放自定义的对象(如自定义的类对象),则必须将其转换成NSData存储:
NSArray *arr = [[NSArray alloc] initWithObjects:@"arr1", @"arr2", nil] [mySettingData setObject:arr forKey:@"arrItem"]; [mySettingData setObject:@"admin" forKey:@"user_name"]; [mySettingData setBOOL:@YES forKey:@"auto_login"]; [mySettingData setInteger:1 forKey:@"count"];
NSUserDefaults *mySettingDataR = [NSUserDefaults standardUserDefaults]; NSLog(@"arrItem=%@", [mySettingDataR objectForKey:@"arrItem"]); NSLog(@"user_name=%@", [mySettingDataR objectForKey:@"user_name"]); NSLog(@"count=%d", [mySettingDataR integerForKey:@"count"]);
如果想删除某个数据项,可以使用removeObjectForKey删除数据:
[mySettingData removeObjectForKey:@"arrItem"];
[mySettingData synchronize];
新闻热点
疑难解答