iOS开发之界面间传值

来源:互联网 发布:java错误找不到符号 编辑:程序博客网 时间:2024/05/16 05:58

在iOS开发过程中,界面间数据传输是最为基本的需求,苹果公司为我们开发中提供了多种传值方式,今天我们来谈一谈较为常用的五种方式。
1、属性传值
2、代理传值
3、block传值
4、单例传值
5、通知传值
五种方式各有特点,在不同情况可以选择使用不同的方式,接下来我们就说一说这五种传值方式

一、属性传值

一般来说如果从前一个界面往后一个界面进行传值,属性传值是最简单也是较为方便的一种。
现在有两个视图控制器FirstViewController和SecondViewController,我们的需求是:在FirstViewController输入一段内容,并将内容在SecondViewController中显示。这种情况就可以选择属性传值,操作如下:

我们先在SecondViewController.h文件中设置一个接口,接收由第一个页面传过来的内容。

#import <UIKit/UIKit.h>@interface SecondViewController : UIViewController@property(nonatomic,strong)NSString *str;//传值字符串@end

在SecondViewController.m文件中创建一个label用来显示接收到的内容

[super viewDidLoad];    self.view.backgroundColor = [UIColor orangeColor];    self.navigationItem.title = @"第二页";    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 100, 314, 50)];    label.backgroundColor = [UIColor redColor];    label.text = _str;    [self.view addSubview:label];}

接下来我们就可以利用在FirstViewController页面利用传值字符串str进行传值了

//首先建立一个输入框- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    self.navigationItem.title = @"第一页";    self.firstTF = [[UITextField alloc]initWithFrame:CGRectMake(20, 200, 314, 50)];    self.firstTF.placeholder = @"请输入....";    self.firstTF.borderStyle = UITextBorderStyleLine;    [self.view addSubview:_firstTF];  }//然后进行页面跳转并传值-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    self.secondCV = [SecondViewController new];    [self.navigationController pushViewController:_secondCV animated:YES];    _secondCV.str = _firstTF.text;    _secondCV.delegate = self;}

代理传值

刚刚我们介绍了利用属性从前往后传值,那么如果开发过程中的需求是从后往前进行传值呢?我们可以选择代理传值、block传值或者单例传值等等,这里我们先介绍下代理传值。
我们仍使用上面的两个页面,但是开发需求变为:在SecondViewController页面输入一段内容,回调到FirstViewController上显示
首先我们需要在SecondViewController.h文件中定义协议并声明代理

#import <UIKit/UIKit.h>//代理@protocol secondViewControllerDelegate <NSObject>-(void)pass:(NSString*)Volue;@end@interface SecondViewController : UIViewController@property(nonatomic,strong)UITextField *secondTF;@property(nonatomic,weak)id<secondViewControllerDelegate> delegate;  //声明代理@end

在SecondViewController.m文件中创建输入框,并实现代理方法

- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    self.navigationItem.title = @"第二页";//创建输入框    _secondTF = [[UITextField alloc]initWithFrame:CGRectMake(30, 300, 314, 50)];    _secondTF.placeholder = @"再次输入...";    _secondTF.borderStyle = UITextBorderStyleLine;    [self.view addSubview:_secondTF];}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//跳转页面    [self.navigationController popViewControllerAnimated:YES];//代理传值    [_delegate pass:_secondTF.text];}

然后我们需要FirstViewController.m文件中指定代理并实现代理方法

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    self.secondCV = [SecondViewController new];    [self.navigationController pushViewController:_secondCV animated:YES];//指定代理    _secondCV.delegate = self;}//实现代理方法,接收传过来的内容-(void)pass:(NSString *)Volue{    _label .text = Volue;}//创建label显示接收内容- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];   _label = [[UILabel alloc]initWithFrame:CGRectMake(30, 100, 314, 50)];    _label.backgroundColor = [UIColor redColor];    [self.view addSubview:_label];}

block传值

开发需求为:在SecondViewController页面输入一段内容,回调到FirstViewController上显示
首先我们要在SecondViewController.h文件中定义并声明block,

#import <UIKit/UIKit.h>//定义有参无返回值的匿名函数(传递字符串)typedef void (^MyBlock)(NSString *);@interface SecondViewController : UIViewController//MRC:block的语义设置是copy,把block从栈区拷贝到堆区,使用完之后,在dealloc释放//ARC:语义设置使用strong即可@property(nonatomic,copy)MyBlock block;@end

在SecondViewController.m文件中进行传值

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //block的调用    self.block(_textField.text);    [self.navigationController popViewControllerAnimated:YES];}

在FirstViewController页面接收传递过来的内容

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    SecondViewController *sencndVC = [SecondViewController new];    //通过回调将传进来的字符串赋值给label    __block typeof(self)temp = self;//内存优化    sencndVC.block = ^(NSString *string){       temp.Label.text = string;    };    [self showViewController:sencndVC sender:nil];    [sencndVC release];}

单例传值

单例传值可以理解为定义一个全局静态变量进行传值,我们同样使用上面需求,将第二个页面的内容传入第一个页面并显示。
首先定义一个单例类,并创建一个对外接口。

#import <Foundation/Foundation.h>@interface Datahandle : NSObject@property(nonatomic,strong)NSString *passVolud;+(instancetype)sharedHadle;@end

在Datahandle.m文件中实现

#import "Datahandle.h"@implementation Datahandlestatic Datahandle *datahandle = nil;//创建单例+(instancetype)sharedHadle{    if (nil == datahandle) {        datahandle = [[Datahandle alloc]init];    }    return datahandle;}    //也可以使用多线程创建//    static dispatch_once_t onceToken;//    dispatch_once(&onceToken, ^{//    datahandle = [[Datahandle alloc]init];//    });////   return datahandle;@end

在第二个页面secondViewController.m文件中创建输入框并在页面跳转时将输入框内容传值

- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    self.secondTF = [[UITextField alloc]initWithFrame:CGRectMake(30, 300, 314, 50)];    _secondTF.borderStyle = UITextBorderStyleLine;    [self.view addSubview:_secondTF];    self.navigationItem.title = @"第二页";   }-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    [self.navigationController popViewControllerAnimated:YES];     Datahandle *data = [Datahandle sharedHadle];     data.passVolud = _secondTF.text;     NSLog(@"%@",data.passVolud);}

在第一个页面接收并显示内容,这里有一点需要注意从第二个页面传回来的时候,传值有个加载顺序,只能在即将加载中显示

-(void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    Datahandle *data = [Datahandle sharedHadle];//接收字符串并显示    _firstTF.text = data.passVolud;}- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    self.firstTF = [[UITextField alloc]initWithFrame:CGRectMake(20, 300, 314, 50)];    _firstTF.borderStyle = UITextBorderStyleLine;    [self.view addSubview:_firstTF];    self.navigationItem.title = @"第一页";}

通知传值

在各控制器之间传值除了代理模式外,通知也是较为快捷,方便的方式之一。
我们假定这样一个场景,有两个页面,需要从第二个页面传递数值到第一个页面,这时我们就可以选用通知模式。
首先我们在第二个页面创建一个按钮,点击按钮跳转页面并发送通知。

- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor  = [UIColor orangeColor];    self.navigationItem.title = @"第二页";  //创建按钮    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];    button.backgroundColor = [UIColor greenColor];    button.frame = CGRectMake(100, 100, 100, 80);    [button addTarget:self action:@selector(abc:) forControlEvents:UIControlEventTouchDown ];    [self.view addSubview:button];}-(void)abc:(UIButton *)sender{    [self.navigationController popToRootViewControllerAnimated:YES];    //创建字典    NSDictionary *dict = [NSDictionary dictionaryWithObject:@"passVole" forKey:@"key"];    //发送通知    [[NSNotificationCenter defaultCenter]postNotificationName:@"volue" object:nil userInfo:dict];}

在第一个页面添加观察者,用来监听通知事件

- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor redColor];    //注册通知(等待接收消息)    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volue:) name:@"volue" object:nil];}-(void)volue:(NSNotification *)sender{    //打印通知传过来的数值    NSLog(@"%@",sender.userInfo[@"key"]);}

通知就是这么简单,发送通知,接收通知,简单几步就实现了界面间的通信。但是使用通知时有一点必须注意,使用完之后必须及时移除,避免造成混乱。

原创粉丝点击