1.正向傳值:
概念:RootViewController(RVC) (傳向)—> SubViewController(SVC)
方法:正向傳值又稱屬性傳值仪媒,在SVC中定義專門用來傳值的屬性(如傳字符串屬性的值氢架,則定義NSString類型屬性)
RVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
KGSubViewController * svc = [[KGSubViewController alloc]init];
svc.string = tf.text;
[self presentViewController:svc animated:YES completion:nil];
}
SVC.h文件:
@property(nonatomic,retain) NSString * string;//定義一個屬性,用來接收數(shù)據(jù)
SVC.m文件:
label.text = self.string;//在這里,將傳進(jìn)來的數(shù)據(jù),賦給label
2.反向傳值:
(1)使用對象傳值:
概念:在SVC.h文件中定義RVC類型的對象作為屬性,以方便調(diào)用/以賦值
RVC.h文件:
-(void)backValue:(NSString *)string color:(UIColor *)color;//聲明一個方法用來進(jìn)行傳值
RVC.m文件:
-(void)backValue:(NSString *)string color:(UIColor *)color//定義返回參數(shù)的函數(shù)
{
label.text = string;//將參數(shù)的值賦給label
label.textColor = color; //將顏色賦給賦給lable的字體顏色
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
KGSubViewController * svc = [[KGSubViewController alloc]init];
svc.rvc = self;//讓B持有A
[self presentViewController:svc animated:YES completion:nil];
}
SVC.h文件:
@property(nonatomic,retain) KGRootViewController * rvc;//創(chuàng)建一個Root對象
SVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.rvc backValue:tf.text color:[UIColor redColor]];//在銷毀之前,做一些回傳數(shù)據(jù)的事
[self dismissViewControllerAnimated:YES completion:nil];
}
(2)使用target/selector傳值:
概念:在SVC.h文件中聲明target/selector屬性,以傳遞變量和方法托享,避免對象傳值中信息完全暴露的危險
RVC.m文件:
-(void)backValue:(NSString *)string
{
label.text = string;//回傳數(shù)據(jù)方法
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
KGSubViewController * svc = [[KGSubViewController alloc]init];
svc.target = self;//將回傳對象進(jìn)行指定
svc.selector = @selector(backValue:);//將回傳方法,進(jìn)行指定
[self presentViewController:svc animated:YES completion:nil];
}
SVC.h文件:
//在這里定義兩個屬性,用來接收目標(biāo)和方法,用來進(jìn)行反向傳值
@property(nonatomic,retain) id target; //接收要回傳的對象
@property(nonatomic,assign) SEL selector;//接收回傳數(shù)據(jù)的方法
SVC.m 文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//在銷毀之前,將數(shù)據(jù)回傳回去
//利用保存的target 和 action 來進(jìn)行回傳
if ([self.target respondsToSelector:self.selector]) {
[self.target performSelector:self.selector withObject:tf.text];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
(3)使用協(xié)議代理傳值:
概念:要SVC—>RVC,則需要RVC遵守SVC的協(xié)議俊卤,實現(xiàn)協(xié)議中的方法,才能將值反向傳遞
RVC.m文件:
#import "KGSubViewController.h"http://因為協(xié)議制定在B的.h文件里,所以在導(dǎo)入.h文件時,協(xié)議也一起導(dǎo)入
//實現(xiàn)協(xié)議 方法
-(void)backValue:(NSString *)string
{
label.text = string;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
KGSubViewController * svc = [[KGSubViewController alloc]init];
svc.delegate = self;//讓A同意B所提出的協(xié)議條件
[self presentViewController:svc animated:YES completion:nil];
}
SVC.h文件:
@protocol BackValue <NSObject>//在B頁面里,制定一個協(xié)議
-(void)backValue:(NSString *)string;//回傳數(shù)據(jù)的協(xié)議方法
@end
@property(nonatomic,weak) id < BackValue > delegate;
SVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.delegate backValue:tf.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
(4)使用系統(tǒng)自帶的completion block傳值:
概念:利用系統(tǒng)自帶的completion block函數(shù)進(jìn)行傳值
RVC.h文件:
@property(nonatomic,retain) UILabel * label;//用來反向接收數(shù)據(jù)
RVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
KGSubViewController * svc = [[KGSubViewController alloc]init];
svc.rvc = self;
//OC中,block用來去指向一個匿名的語句塊,從而可以當(dāng)成函數(shù)還調(diào)用該語句塊
//這個方法的第三個參數(shù)是一個block,意思說,當(dāng)彈出來的svc顯示完成后,執(zhí)行block里的內(nèi)容
[self presentViewController:svc animated:YES completion:^{
svc.textField.text = self.label.text;//正向傳值
}];
}
SVC.h文件:
@property(nonatomic,retain) UITextField * textField;//用來正向接收數(shù)據(jù)
@property(nonatomic,retain) KGRootViewController * rvc;//一個反向數(shù)據(jù)
SVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self dismissViewControllerAnimated:YES completion:^{
self.rvc.label.text = self.textField.text;}];
}
(5)使用自定義的block傳值:
概念:因為block相當(dāng)于匿名函數(shù)指針血筑,是一段方法的代碼塊绘沉,所以只要有這個指針就可以調(diào)用該方法,而這個方法具體要實現(xiàn)什么功能可以視情況而定豺总,相實現(xiàn)什么方法就定義什么方法
RVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
KGSubViewController * svc = [[KGSubViewController alloc]init];
svc.block = ^(NSString * string){//給block賦值,做用是讓svc知道block指向的代碼塊的功能
label.text = string;};
[self presentViewController:svc animated:YES completion:nil];
}
SVC.h文件:
@property(nonatomic,copy)void (^block)(NSString *);//定義一個block的屬性
SVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if(self.block){
self.block(tf.text);//執(zhí)行block
}
[self dismissViewControllerAnimated:YES completion:nil];
}