本節主要來講解如何使用委托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://[email protected]"]];
撥號(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];
新聞熱點
疑難解答