1、 通知傳值-一般常用於返回界面的時候,把返回前界面的值傳到返回後界面。 2、block 反向傳值 前一個界面獲取後一個界面傳過來的值 3、屬性傳值(一般適用於前一個界面傳值給後一個界面) 4、數據持久化傳值 NSUserDefaults是數據持久化的一種主要做存儲使用。 ...
1、 通知傳值-一般常用於返回界面的時候,把返回前界面的值傳到返回後界面。
//前一個界面 //註冊通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:@"notification" object:nil]; // Do any additional setup after loading the view. } //執行通知方法 - (void) notification:(NSNotification *)notifi{ NSLog(@"++++++"); NSLog(@"%@",notifi.userInfo); } //移除通知 -(void) dealloc{ //第一種 移除該控制器所有的通知 [[NSNotificationCenter defaultCenter] removeObserver:self]; //第二種 移除該控制器下名為"notification"的通知 // [[NSNotificationCenter defaultCenter] removeObserver:self name:@"notification" object:nil]; }
//後一個界面 //創建傳值信息 NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:@"val1",@"key1",@"val2",@"key2", nil]; //創建通知 NSNotification *notification=[NSNotification notificationWithName:@"notification" object:nil userInfo:dict]; //通過通知中心發送通知 [[NSNotificationCenter defaultCenter] postNotification:notification];
2、block 反向傳值
前一個界面獲取後一個界面傳過來的值
//後一個界面 .h文件 @interface jViewController : UIViewController //定義block @property (nonatomic,copy) void (^NextViewControllerBlock)(NSString *tfText); @end .m文件 -(void) btnClick:(UIButton *)btn{ //判斷block是否為空 if (self.NextViewControllerBlock) { self.NextViewControllerBlock(@"我是後一個界面傳的值"); } [self.navigationController popViewControllerAnimated:YES]; }
//前一個界面 //.m文件 -(void) btnClick:(UIButton *)btn{ jViewController *nextVC = [[jViewController alloc]init]; nextVC.NextViewControllerBlock = ^(NSString *tfText){ NSLog(@"++++%@",tfText); }; [self.navigationController pushViewController:nextVC animated:YES]; }
3、屬性傳值(一般適用於前一個界面傳值給後一個界面)
//後一個界面 //首先在.h定義獲值屬性(就是要有接收傳值過來的屬性) @interface jViewController : UIViewController @property( copy,nonatomic)NSString *str; @end //.m -(void) btnClick:(UIButton *)btn{ NSLog(@"++++%@",_str); }
//前一個界面 -(void) btnClick:(UIButton *)btn{ //跳轉到後一個界面,也把值傳過去 jViewController *nextVC = [[jViewController alloc]init]; nextVC.str=@"我是第一個界面傳給第二個界面的值"; [self.navigationController pushViewController:nextVC animated:YES]; }
4、數據持久化傳值
NSUserDefaults是數據持久化的一種主要做存儲使用。
NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults]; //存儲數據 [userDefaults setObject:<#(nullable id)#> forKey:<#(nonnull NSString *)#>]; [userDefaults setInteger:<#(NSInteger)#> forKey:<#(nonnull NSString *)#>]; [userDefaults setBool:<#(BOOL)#> forKey:<#(nonnull NSString *)#>]; [userDefaults setURL:<#(nullable NSURL *)#> forKey:<#(nonnull NSString *)#>]; [userDefaults setFloat:<#(float)#> forKey:<#(nonnull NSString *)#>]; [userDefaults setDouble:<#(double)#> forKey:<#(nonnull NSString *)#>]; [userDefaults setValue:<#(nullable id)#> forKey:<#(nonnull NSString *)#>]; //讀取數據 [userDefaults objectForKey:<#(nonnull NSString *)#>]; [userDefaults integerForKey:<#(nonnull NSString *)#>]; [userDefaults boolForKey:<#(nonnull NSString *)#>]; [userDefaults URLForKey:<#(nonnull NSString *)#>]; [userDefaults floatForKey:<#(nonnull NSString *)#>]; [userDefaults doubleForKey:<#(nonnull NSString *)#>]; [userDefaults valueForKey:<#(nonnull NSString *)#>];