多VC之间的传值
传值:将一个数据存储到另一对象的属性中过程
- 正向传值:当A控制器推出B控制器的同时,A给B传值叫做正向传值 ,实现的关键点:B需要公开一个属性后,A在推出显示B之前,将数据赋给B公开的那个属性即可
step1:B公开属性
step2:A在推出B之前,给B公开的属性赋值
step3:B显示数据
注意:
每个控制器完全控制器自己的那个视图,不要让其他控制器来修改自己的视图,即不要将视图公开,而是让控制器公开标签这种实现传值的方式,不是不正确,而是不推荐
AViewController.m
- (IBAction)clickButton:(id)sender {
BViewController *bvc = [[BViewController alloc]initWithNibName:@"BViewController" bundle:nil];
bvc.content =self.inputTextField.text;
[self presentViewController:bvc animated:YES completion:nil];
}
BViewController.m另在.h中添加了属性content
self.outputLabel.text = self.content;
- 反向传值:在A控制器推出B控制器后,如果从B返回A的过程中,B给A传值,叫做反向传值
实现关键点:让B有A控制器的引用
step1:B公开属性,存储A的引用
step2:A公开方法,方法内完成数据的接受及显示
step3:A在推出B之前,将自己的引用存在B公开的属性中
step4:B在返回之前,调用A的公开方法
AViewController.h
@property(nonatomic,strong)NSString *editContent;
AViewController.m
@implementation AViewController
- (IBAction)editButtonClick:(id)sender {
BViewController *bvc = [[BViewController alloc]init];
//把自己传到B对象中
bvc.avc = self;
[self presentViewController:bvc animated:NO completion:nil];
}
//view将要显示是调用
-(void)viewWillAppear:(BOOL)animated
{ //将回传的内容 显示出来
self.label.text = self.editContent;
}
//view创建并加载时调用
- (void)viewDidLoad {
[super viewDidLoad];}
BViewController.h
@property(nonatomic,strong)AViewController *avc;
BViewController.m
- (IBAction)finishEditButtonClick:(id)sender {
//将内容传给A
self.avc.editContent = self.editField.text;
[self dismissViewControllerAnimated:NO completion:nil];
}
通过方法反向传值
AViewController.h
//给A界面 公开一个方法 在B界面返回A界面的时候,在B界面通过A界面对象调用该方法
-(void)callBackValue:(NSString*)backValue;
AViewController.m
#import "AViewController.h"
#import "BViewController.h"
@interface AViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation AViewController
- (IBAction)editButtonClick:(id)sender {
BViewController *bvc = [[BViewController alloc]init];
//把自己传到B对象中
bvc.avc = self;
[self presentViewController:bvc animated:NO completion:nil];
}
//等待着在B界面中调用,在B界面中调用该方法,会把B的内容通过参数传入到该方法中
-(void)callBackValue:(NSString*)backValue
{ //将B 回传的值 显示出来
self.label.text = backValue;
}
//view创建并加载时调用
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
BViewController.h
@property(nonatomic,strong)AViewController *avc;
BViewController.m
@implementation BViewController
- (IBAction)finishEditButtonClick:(id)sender {
[self.avc callBackValue:self.editField.text];
[self dismissViewControllerAnimated:NO completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
针对上面的步骤进行改良,使用代理模式实现的效果更通用
委托(B):传值方
代理(A): 接受方
委托方需要完成的三件事
1:定义委托协议
2:声明委托协议类型的属性
3:合适的时机通知代理人执行委托协议中的方法
代理方法需要完成的三件事
1.遵守委托协议
2.实现委托协议中的方法
3.设置自己为委托方的代理人
注意:委托协议中方法的第一个参数应该是委托方本身
AViewController.m
#import "AViewController.h"
#import "BViewController.h"
//代理方第一件事 遵守协议
@interface AViewController ()<BViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@end
@implementation AViewController
//代理方第二件事 实现协议中的代理方法
-(void)bViewController:(BViewController*)bvc backVale:(NSString*)backValue
{
self.outputLabel.text = backValue;
}
- (IBAction)gotoB:(id)sender {
BViewController *bvc = [[BViewController alloc]init];
//代理方第三件事 设置委托方的代理人为当前对象
bvc.delegate =self;
[self presentViewController:bvc animated:NO completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
BViewController.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
//@class 后面内容 不管是不是一个真正的类型,在这里先当一个类型取用,但是注意:注意如果没有这个类型 在编译会报错
@class BViewController;
//委托方第一件事 指定协议 协议名 是 委托方类名+Delegate
@protocol BViewControllerDelegate <NSObject>
-(void)bViewController:(BViewController*)bvc backVale:(NSString*)backValue;
@end
@interface BViewController : UIViewController
//委托方第二件事 声明代理人属性 协议类型的属性
@property(nonatomic,weak)id<BViewControllerDelegate>delegate;
@end
BViewController.m
- (IBAction)backA:(id)sender {
//委托方第三件事 适当的时机 通知代理人执行代理方法
[self.delegate bViewController:self backVale:self.inputTextField.text];
[self dismissViewControllerAnimated:NO completion:nil];
}






网友评论