iOS开发之界面间传值

在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传值

基于其他几种传值方式,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 Datahandle
static 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"]);
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容