1.正向傳值
屬性傳值
在B類中定義屬性用于接收A類傳來的數據
2.反向傳值(回調)
1)利用對象反向傳值
將A類對象定義成B類的屬性進行傳值
如,通過KGSubViewController(B)修改 KGRootViewController(A)的Label
KGRootViewController
//在KGRootViewController中定義函數接收返回的數據
-(void)backValue:(NSString *)string color:(UIColor *)color{
//將參數的值賦給
label label.text = string;
//將顏色賦給賦給lable的字體顏色
label.textColor = color;
}
界面跳轉至KGSubViewController,并將KGRootViewController對象傳到KGSubViewController中
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
KGSubViewController * svc = [[KGSubViewController alloc]init];
//讓B持有A
svc.rvc = self;
[self presentViewController:svc animated:YES completion:nil];
}
KGSubViewController
//創建一個Root對象@property(nonatomic,retain) KGRootViewController * rvc;//在銷毀之前,做一些回傳數據的事-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.rvc backValue:tf.text color:[UIColor redColor]]; [self dismissViewControllerAnimated:YES completion:nil];}
2)使用TargetAction反向傳值
通過指定回傳方法和回傳對象進行傳值
仍以上例為例,實現方法為:
KGRootViewController
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
KGSubViewController * svc = [[KGSubViewController alloc]init];
//將回傳對象進行指定 svc.target = self;
//這里的self是指rootviewcontrller
//將回傳方法,進行指定
svc.selector = @selector(backValue:);
[self presentViewController:svc animated:YES completion:nil];
}
-(void)backValue:(NSString *)string{
//回傳數據方法
label.text = string;
}
KGSubViewController
//在這里定義兩個屬性,用來接收目標和方法,用來進行反向傳值//接收要回傳的對象
@property(nonatomic,retain) id target;
//接收回傳數據的方法
@property(nonatomic,assign) SEL selector;
//在銷毀之前,將數據回傳回去
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//利用保存的target 和 action 來進行回傳
if ([self.target respondsToSelector:self.selector]) {
[self.target performSelector:self.selector withObject:tf.text];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
3)使用協議代理反向傳值
協議代理是回調中常用的方法,顧名思義,把某個對象要做的事情委托給別的對象去做。那么別的對象就是這個對象的代理,代理它來打理要做的事情。使用協議代理不僅可以正向傳值,亦可反向傳值。在使用中,需要注意哪個類是委托方,哪個類是代理方。總之:誰傳值誰是委托方
如,通過KGSubViewController改變KGRootViewController中label值
在KGSubViewController頁面里,制定一個協議
@protocol BackValue
//回傳數據的協議方法
-(void)backValue:(NSString *)string;@end@property(nonatomic,retain) id < BackValue > delegate;
在KGSubViewController使代理方實現方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//代理方實現協議中的方法
[self.delegate backValue:tf.text]; [self dismissViewControllerAnimated:YES
completion:nil];
}
在KGRootViewController中實現協議
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ KGSubViewController * svc = [[KGSubViewController alloc]init]; //讓A同意B所提出的協議條件 svc.delegate = self; [self presentViewController:svc animated:YES completion:nil];}//實現協議 方法-(void)backValue:(NSString *)string{ label.text = string;}
4)使用系統自帶的Block進行反向傳值
block是代碼塊的對象,類似函數指針,調用block如同調用代碼塊中的代碼,利用block可以實現回調。
在系統提供方法中,有很多使用block方法,如界面跳轉presentViewController時
將KGRootViewController中label.text值傳給KGSubViewController textField.text
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ KGSubViewController * svc = [[KGSubViewController alloc]init]; svc.rvc = self; //OC中,block用來去指向一個匿名的語句塊,從而可以當成函數還調用該語句塊 //這個方法的第三個參數是一個block,意思說,當彈出來的svc顯示完成后,執行block里的內容 [self presentViewController:svc animated:YES completion:^{ //正向傳值 svc.textField.text = self.label.text; }];}
將KGSubViewController textField.text值傳給 KGRootViewController中label.text
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self dismissViewControllerAnimated:YES completion:^{ self.rvc.label.text = self.textField.text; }];}
5)使用自定義Block反向傳值
KGSubViewController
//定義一個block的屬性@property(nonatomic,copy)void (^block)(NSString *);-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //執行block self.block(tf.text); [self dismissViewControllerAnimated:YES completion:nil];}
KGRootViewController
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ KGSubViewController * svc = [[KGSubViewController alloc]init]; //給block賦值,做用是讓svc知道block指向的代碼塊的功能 svc.block = ^(NSString * string){ label.text = string; }; [self presentViewController:svc animated:YES completion:nil];}
*3.雙向產值*1)使用NSUserDefaults進行數據傳值
NSUserDefaults紀錄本地一些輕量級的數據,是單例類。其通過userDefaults對象來將選中按鈕的索引給保存到NSUserDefaults的plist文件中。這個文件實際上是一個plist文件,在沙盒中的/Library/Preferences/NSUserDefaults.plist 這個位置
當KGSubViewController調用viewWillAppear時讀取沙盒內容
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //獲取NSUserDefaults文件,讀取里面內容,來決定讓哪一個按鈕進行選中 NSUserDefaults * ud = [NSUserDefaults standardUserDefaults]; //通過存的時候的key值來進行取值 NSString * value = (NSString *)[ud objectForKey:@"ButtonIndex"]; //計算出按鈕的tag值 int buttonIndex = 1000 + value.intValue; //利用tag值取到按鈕 UIButton * button = (UIButton *)[self.view viewWithTag:buttonIndex]; button.selected = YES;}
KGRootViewController點擊button將點擊按鈕的索引存入沙盒中
-(void)buttonClick:(UIButton *)button{ for (int i = 1;i<6; i++) { UIButton * btn = (UIButton *)[self.view viewWithTag:1000+i]; if (btn.selected == YES) { btn.selected = NO; } } button.selected = YES; //將每次點擊選中的按鈕索引給存起來 NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; NSLog(@"%@",[NSBundle mainBundle]); //通過userDefaults對象來將選中按鈕的索引給保存到NSUserDefaults的plist文件中 //這個文件實際上是一個plist文件,在沙盒中的/Library/Preferences/NSUserDefaults.plist 這個位置 [userDefaults setObject:[NSString stringWithFormat:@"%d",button.tag - 1000] forKey:@"ButtonIndex"]; //回寫文件 [userDefaults synchronize]; //synchronize:使同步,同時發生}
KGRootViewController
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //獲取NSUserDefaults文件,讀取里面內容,來決定讓哪一個按鈕進行選中 NSUserDefaults * ud = [NSUserDefaults standardUserDefaults]; //通過存的時候的key值來進行取值 NSString * value = (NSString *)[ud objectForKey:@"ButtonIndex"]; label.text = [NSString stringWithFormat:@"上次按下的是第 %@ 個按鈕",value];}
2)使用系統的UIApplication單例進行傳值
單例類在程序中只創建一次實例對象,利用這個特點可以進行傳值。在iOS開發中,UIApplication就是個單例類,利用他可實現傳值。
在UIApplication定義存儲內容
//創建一個公共數組,用來存放數據,@property(nonatomic,retain) NSMutableArray * array;
在相應ViewController中使用
//應該去利用系統的UIApplication這個單例來獲取系統已經創建好的那個AppDelegate里的對象//這是一個單例 對象,在整個程序中,只有一個唯一的實例存在UIApplication * application = [UIApplication sharedApplication];//通過這個單例對象來找到它持有的AppDelegate對象KGAppDelegate * appDelegate = application.delegate;//通過appDelegate對象拿到他的數組[appDelegate.array addObject:self.title];
3)使用自己的單例類進行傳值
除了使用系統自帶的單例類外,還可以使用自定義單例類用來傳值
自定義單例類KGMyApplication
//為application類,添加一個屬性,用來接收MyAppDelegate對象@property(nonatomic,retain) KGMyAppDelegate * delegate;
//使用這個方法來實例一個單例對象+(KGMyApplication *)sharedMyApplication;+(KGMyApplication *)sharedMyApplication{ static KGMyApplication * obj = nil; if (!obj) { //給obj創建單例對象 obj = [[KGMyApplication alloc]init];
//給單例對象的delegate屬性賦值 obj.delegate = [[KGMyAppDelegate alloc]init]; } return obj;}
定義KGMyAppDelegate類保存數據
//這個類不是一個單例類,但是,需要在這里保存數據
@property(nonatomic,retain)NSMutableArray * array;
在相應ViewController中使用
//首先獲取MyApplication的單例對象KGMyApplication * app = [KGMyApplication sharedMyApplication];//通過這個單例對象里的delegate屬性來獲取MyAppDelegate對象KGMyAppDelegate * appDelegate = app.delegate;//獲取MyAppDelegate里的數組NSMutableArray * array = [appDelegate array];static int n = 0;[array addObject:[NSString stringWithFormat:@"VC1-%d",n++]];
4)使用通知中心NSNotificationCenter進行傳值
通知由通知中心發送,通知中心在整個工程中只有一個。通知中心可以發送多條消息,可以在整個工程中的任何位置接收消息,通過通知中心的名稱區分消息。
一個簡單的應用,從界面2中發送通知,界面1中接收
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomeThing:) name:@"xiaoxi" object:nil];- (void)doSomeThing:(NSNotification *)noti{ NSLog(@"收到了"); label.text = [noti.userInfo objectForKey:@"key"];}
KGSecondViewController
NSNotification *noti = [[NSNotification alloc]initWithName:@"xiaoxi" object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@", btn.titleLabel.text],@"key", nil]];
// 發送
[[NSNotificationCenter defaultCenter] postNotification:noti];