当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS中正向、逆向传值的方法总结

iOS中正向、逆向传值的方法总结

2019年07月23日  | 移动技术网IT编程  | 我要评论

在广州从化街口找情妇,希金诺,ps古铜色

前言

本文主要介绍了关于ios正向、逆的相关内容,对各位ios开发者们具有一定的参考学习价值,下面话不多说了,来一起看看详细的介绍吧


ios 开发中经常需要在页面间传递数据,也就是 值传递。常见的有的是 正向传递 也有 逆向传递。

那么如何进行值传递呢 ?!

一、正向传递(one ---> two)

正向传值只需要在第一个视图控制器中“拿到”第二个视图控制器,然后在跳转之前将第一个视图控制器的值赋值给第二个视图控制器所需要获得这个值的属性。

需求

假设现在有 控制器one 和 控制器two,在 one 、two 控制器中都有个 uitextfield ,在控制器 one 的 uitextfield 中输入你想传递的值,然后跳转到 two 控制器,将 one 中的 uitextfield 的文本显示在 two 的 uitextfield 上。

one 传到 two

one 控制器 (给要跳转的控制器属性赋值)

#import "oneviewcontroller.h"
#import "twoviewcontroller.h"

@interface oneviewcontroller ()

@property (weak, nonatomic) iboutlet uitextfield *onetextf;

@end

@implementation oneviewcontroller

- (void)viewdidload {
 [super viewdidload];
 
 self.navigationitem.title = @"onevc";
}

- (ibaction)pushtotwovc:(id)sender
{
 twoviewcontroller *twovc = [[twoviewcontroller alloc] init];
 // 重点!给 twoviewcontroller 的 value 属性赋值
 twovc.value = _onetextf.text;
 [self.navigationcontroller pushviewcontroller:twovc animated:yes];
}

@end

two 控制器

一、先声明一个要接受值的属性

#import <uikit/uikit.h>

@interface twoviewcontroller : uiviewcontroller

/** a 控制器传过来的值 */
@property (strong, nonatomic) nsstring *value;

@end

二、在 two 控制器中赋值

#import "twoviewcontroller.h"

@interface twoviewcontroller ()

@property (weak, nonatomic) iboutlet uitextfield *twotextf;

@end

@implementation twoviewcontroller

- (void)viewdidload {
 [super viewdidload];
 
 self.navigationitem.title = @"twovc";
 // 重点!给 twotextf 赋值
 _twotextf.text = _value;
}

@end

一、逆向传递(two---> one)

那么反过来从 two 控制器如何向 one 控制器传值呢?

常用的有 代理、通知、单例、block 。

two 传到 one

方式一、代理

1、需要在控制器 two 制定一套协议。

2、在控制器 two 声明一个遵守协议的代理对象。

3、在 two 中 **恰当** 的地方 **使用代理对象调用代理方法**。

4、在 one 中遵守协议、设置代理对象、实现代理方法。

第1、2步

#import <uikit/uikit.h>
@class twoviewcontroller;

// 制订协议
@protocol twoviewcontrollerdelegate<nsobject>
@optional

- (void)twoviewcontroller:(twoviewcontroller *)twovc sendvalue:(nsstring *)strvalue;

@end

@interface twoviewcontroller : uiviewcontroller

/** 代理对象(任意遵守 twoviewcontrollerdelegate 的对象) */
@property (weak, nonatomic) id<twoviewcontrollerdelegate> delegate;

@end

第3步

#import "twoviewcontroller.h"

@interface twoviewcontroller ()

@property (weak, nonatomic) iboutlet uitextfield *twotextf;

@end

@implementation twoviewcontroller

- (void)viewdidload {
 [super viewdidload];
 
 self.navigationitem.title = @"twovc";
 // 自定义返回按钮(为了监听返回事件)
 uibutton *backbtn = [[uibutton alloc] init];
 backbtn.frame = cgrectmake(0, 0, 50, 50);
 [backbtn settitle:@"返回" forstate:uicontrolstatenormal];
 [backbtn settitlecolor:[uicolor bluecolor] forstate:uicontrolstatenormal];
 [backbtn addtarget:self action:@selector(navback:) forcontrolevents:uicontroleventtouchupinside];
 self.navigationitem.leftbarbuttonitem = [[uibarbuttonitem alloc] initwithcustomview:backbtn];
}

- (void)navback:(uibutton *)backbtn
{
 /** 使用代理对象调用代理方法
  1、判断代理对象是否实现代理方法
  2、使用代理对象调用代理方法(传值)
 */
 if ([self.delegate respondstoselector:@selector(twoviewcontroller:sendvalue:)]) {
 [self.delegate twoviewcontroller:self sendvalue:@"i'm from twovc"];
 }
 
 [self.navigationcontroller popviewcontrolleranimated:yes];
}

@end

第4步

#import "oneviewcontroller.h"
#import "twoviewcontroller.h"

// 1、遵守协议<twoviewcontrollerdelegate>
@interface oneviewcontroller ()<twoviewcontrollerdelegate>

@property (weak, nonatomic) iboutlet uitextfield *onetextf;

@end

@implementation oneviewcontroller

- (void)viewdidload {
 [super viewdidload];
 
 self.navigationitem.title = @"onevc";
}

- (ibaction)pushtotwovc:(id)sender
{
 twoviewcontroller *twovc = [[twoviewcontroller alloc] init];
 // 2、设置代理对象
 twovc.delegate = self;
 [self.navigationcontroller pushviewcontroller:twovc animated:yes];
}

// 3、实现代理方法
- (void)twoviewcontroller:(twoviewcontroller *)twovc sendvalue:(nsstring *)strvalue
{
 // 4、赋值
 self.onetextf.text = strvalue;
}

@end

方式二、通知

1、在控制器 two 发送通知。

2、在控制器 one 添加观察者(接收通知)。

3、在控制器 one 的 dealloc 方法中移除观察者。

第1步(two控制器中)

- (void)navback:(uibutton *)backbtn
{
 /** 发送通知 */
 [[nsnotificationcenter defaultcenter] postnotificationname:@"sendvalue" object:nil userinfo:@{@"value": @"i'm from twovc"}];
 
 [self.navigationcontroller popviewcontrolleranimated:yes];
}

第2步(one控制器中)

- (void)viewdidload {
 [super viewdidload];
 
 self.navigationitem.title = @"onevc";
 // 1、添加观察者对象 self
 [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(gettwovcvalue:) name:@"sendvalue" object:nil];
}

// 接收到通知时执行的方法
- (void)gettwovcvalue:(nsnotification *)noti
{
 self.onetextf.text = noti.userinfo[@"value"];
}

第3步(one控制器中)

// 移除观察者
- (void)dealloc
{
 [[nsnotificationcenter defaultcenter] removeobserver:self];
}

方式三、单例

1、定义一个单例并声明一个属性(传递值用)。

2、在控制器 two 给单例属性赋值 。

3、在控制器 one 中利用单例的属性取出值。

第1步

testsingleton.h

#import <foundation/foundation.h>

@interface testsingleton : nsobject

/** 构造方法,返回单例对象 */
+ (instancetype)shareinstance;
/** 用于传递值 */
@property (strong, nonatomic) nsstring *values;

@end

testsingleton.m

#import "testsingleton.h"

@implementation testsingleton

static testsingleton *_instance = nil;

+ (instancetype)shareinstance
{
 static dispatch_once_t oncetoken ;
 dispatch_once(&oncetoken, ^{
  _instance = [[self alloc] init] ;
 }) ;
 
 return _instance ;
}

@end

第2步

- (void)navback:(uibutton *)backbtn
{
 /** 生成单例并赋值 */
 testsingleton *singleton = [testsingleton shareinstance];
 singleton.values = @"i'm from twovc";
 
 [self.navigationcontroller popviewcontrolleranimated:yes];
}

第3步

- (void)viewwillappear:(bool)animated
{
 [super viewwillappear:animated];
 /** 生成单例并取出值 */
 testsingleton *singleton = [testsingleton shareinstance];
 _onetextf.text = singleton.values;
}

方式四、block

1、在控制器 two 中定义一个 block 属性。

2、在控制器 two 中传值。

3、在控制器 one 中接收值。

第1步:在控制器 two 中定义一个 block 属性。

#import <uikit/uikit.h>

@interface twoviewcontroller : uiviewcontroller

/** 定义一个 block */
@property (copy) void (^block)(nsstring *);

@end

第2步:在控制器 two 中传值。

- (void)navback:(uibutton *)backbtn
{
 /** block 传值 */
 self.block(@"i'm from twovc");
 
 [self.navigationcontroller popviewcontrolleranimated:yes];
}

第3步:在控制器 one 中接收值。

- (ibaction)pushtotwovc:(id)sender
{
 twoviewcontroller *twovc = [[twoviewcontroller alloc] init];
 // 实现 block,接收 twovc 传过来的值
 twovc.block = ^(nsstring *str) {
  self.onetextf.text = str;
 };
 [self.navigationcontroller pushviewcontroller:twovc animated:yes];
}

另外还可以通过 nsuserdefaults 将值储存在本地实现逆向传值等。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网