IOS頁面間得傳值方式

來源:互聯網
上載者:User

一.通過Delegate來傳遞資料

本節主要來講解如何使用委託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)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http:google.com"]]; 
 調用Google地圖(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:@"sms://466453"]];  

調用市集(AppStore)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&amp;amp;mt=8"]];  
   四.使用NSUerDefaults或檔案持久化資料後實現頁面間資料的傳遞 IOS下可以使用NSUserDefaults、sqlite、CoreData幾種常用的方式來儲存資料,其中NSUserDefaults用來儲存類似使用者的配置等這些的資料,後兩者使用者儲存大批量和比較複雜的資料。NSUserDefault的使用比較簡單:  
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添加資料後,它們就變成了全域的變數,App中即可讀寫NSUserDefault中的資料:
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"];   
需要注意的是,NSUserDefaults是定時把緩衝中的資料寫入磁碟的,而不是即時寫入,為了防止在寫完NSUserDefaults後程式退出導致的資料丟失,可以在寫入資料後使用synchronize強制立即將資料寫入磁碟:
[mySettingData synchronize];  
運行上面的語句後,NSUserDefaults中的資料即被寫入到.plist檔案中,如果是在模擬器上運行程式,可以在Mac的/Users/YOUR-USERNAME/Library/Application Support/iPhone Simulator/4.1/Applications/YOUR-APP-DIR/Library/Prefereces目錄下面找到一個檔案名稱為YOUR-Bundle_Identifier.plist的plist檔案,用Xcode開啟該檔案,可以看到剛才寫入的資料。   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.