美文网首页
初步介绍iOS界面间反向传值的几种方法

初步介绍iOS界面间反向传值的几种方法

作者: 呼哮山庄 | 来源:发表于2016-07-31 23:20 被阅读122次

这次主要总结一下界面间的反向传值的主要方法.

1.协议传值

第二个控制器设置协议相关信息
第一个控制器实现协议方法

//第二个控制器
#import <UIKit/UIKit.h>
@protocol JHDelegateViewControllerDelegate <NSObject>
@optional
- (void)delegateViewControllerDidClickwithString:(NSString *)string color:(UIColor *)color;
@end
@interface JHDelegateViewController : UIViewController
@property (nonatomic, assign) id<JHDelegateViewControllerDelegate> delegate;
@end
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if ([self.delegate respondsToSelector:@selector(delegateViewControllerDidClickwithString:color:)]) {
    [self.delegate delegateViewControllerDidClickwithString:self.field.text color:[UIColor greenColor]];
}
[self.navigationController popViewControllerAnimated:YES];
}
//第一个控制器
@interface JHRootViewController ()<JHDelegateViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *delegateBtn;
@end
- (IBAction)pressDelegateVc:(id)sender {
JHDelegateViewController * vc = [[JHDelegateViewController alloc]init];
vc.delegate = self;
[self.navigationController pushViewController:vc animated:YES];
}
- (void)delegateViewControllerDidClickwithString:(NSString *)string color:(UIColor *)color
{
[self.delegateBtn setTitle:string forState:UIControlStateNormal];
[self.delegateBtn setBackgroundColor:color];
}

2.属性反向传值(注意与属性正向传值不同)

//属性传值从后往前传(不推荐)
//SecondViewController  - > 取到textField,text
//       pop(获取FirstVC)
//viewController - >设置titleTextField - >去使用

//第一个控制器
#import <UIKit/UIKit.h>
#import "BaseViewController.h"
@interface ViewController : BaseViewController
@property (nonatomic,strong)UILabel *titleLable;
@end

//第二个控制器
#import "SecondViewController.h"
#import "ViewController.h"
@interface SecondViewController ()
@property (nonatomic,strong)UITextField *textField;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
 [self createSubView];
 }
- (void)createSubView{
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 200, 49)];
_textField.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_textField];
_textField.text = self.titleString;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(100, 400, 100, 100);
[button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)handleButton:(UIButton *)button{
//属性往回传值
NSInteger count = self.navigationController.viewControllers.count;
ViewController *first = self.navigationController.viewControllers[count - 2];
first.titleLable.text = _textField.text;    
[self.navigationController popToRootViewControllerAnimated:YES];    
}

------以下为我收集整理的传值方法

3.APPDelegate

在AppDelegate中定义相关的属性变量
第二个控制器中为AppDelegate中的属性赋值
第一个控制器中在使用的地方取出AppDelegate中的值赋值

//UIApplicationDelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
//AppDelegate属性
@property (nonatomic, copy) NSString * string;
@property (nonatomic, strong) UIColor * color;
@end
//第二个控制器
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//设置数据信息
AppDelegate * app = [UIApplication sharedApplication].delegate;

app.string = self.field.text;
app.color = [UIColor orangeColor];

[self.navigationController popViewControllerAnimated:YES];
}

//第一个控制器---设置
 AppDelegate * app = [UIApplication sharedApplication].delegate;
if (app.string) {
    [self.appBtn setTitle:app.string forState:UIControlStateNormal];
    [self.appBtn setBackgroundColor:app.color];
}

4.控制器传值

第一个控制器.h里面设置相关属性等信息
第二个控制器设置第一个控制器参数,并在对应位置设置相关信息

//第一个控制器
#import <UIKit/UIKit.h>
@interface JHRootViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *vcBtn;
@end
//第二个控制器
#import <UIKit/UIKit.h>
@class ZZYRootViewController;
@interface ZZYVCViewController : UIViewController
@property (nonatomic, strong) ZZYRootViewController * rootVc;
@end
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.rootVc.vcBtn setTitle:self.field.text forState:UIControlStateNormal];
[self.rootVc.vcBtn setBackgroundColor:[UIColor orangeColor]];
[self.navigationController popViewControllerAnimated:YES];
}

5.通知传值

第二个控制器发送通知
第一个控制器接收通知,设置数据

//第二个控制器
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[[NSNotificationCenter defaultCenter]postNotificationName:@"ZZYNotiSendValue" object:nil userInfo:@{@"value":self.field.text,@"color":[UIColor redColor]}];
[self.navigationController popViewControllerAnimated:YES];
}
//第一个控制器
 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setNoti:) name:@"ZZYNotiSendValue" object:nil];
- (void)setNoti:(NSNotification *)noti
{
[self.notiBtn setTitle:noti.userInfo[@"value"] forState:UIControlStateNormal];
[self.notiBtn setBackgroundColor:noti.userInfo[@"color"]];
}

5.Block传值

第二个控制器设置block相关信息
第一个控制器调用block,设置数据

方式1

//第二个控制器
@interface JHBlockViewController : UIViewController
@property (nonatomic, copy) void(^JHMyBlock) (NSString * string , UIColor * color);
@end
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (self.JHMyBlock) {
    self.JHMyBlock(self.field.text,[UIColor lightGrayColor]);
}
[self.navigationController popViewControllerAnimated:YES];
}
//第一个控制器
JHBlockViewController * vc = [[JHBlockViewController alloc]init];
[vc setJHMyBlock:^(NSString * string, UIColor * color) {

    [self.blockBtn setTitle:string forState:UIControlStateNormal];
    [self.blockBtn setBackgroundColor:color];

}];

方式2

//第二个控制器
typedef void(^JHMySecBlock) (NSString * string , UIColor * color);
@interface JHBlockViewController : UIViewController
@property (nonatomic, copy) JHMySecBlock mySecondBlock;
- (void)returnTextBlock:(JHMySecBlock)block;
@end
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (self.mySecondBlock) {
    self.mySecondBlock(self.field.text,[UIColor lightGrayColor]);
}
[self.navigationController popViewControllerAnimated:YES];
}
//第一个控制器
 JHBlockViewController * vc = [[JHBlockViewController alloc]init];
 [vc returnTextBlock:^(NSString *string, UIColor *color) {
    [self.blockBtn setTitle:string forState:UIControlStateNormal];
    [self.blockBtn setBackgroundColor:color];
}];

方式3

//第二个控制器
#import <UIKit/UIKit.h>
typedef void(^JHMyThrBlock) (NSString * string , UIColor * color);
@interface JHBlockViewController : UIViewController
@property (nonatomic, copy) JHMyThrBlock myThrBlock;
@end
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (self.myThrBlock) {
    self.myThrBlock(self.field.text,[UIColor lightGrayColor]);
}
[self.navigationController popViewControllerAnimated:YES];
}
//第一个控制器
JHBlockViewController * vc = [[JHBlockViewController alloc]init];
vc.myThrBlock = ^(NSString * string, UIColor * color){
    [self.blockBtn setTitle:string forState:UIControlStateNormal];
    [self.blockBtn setBackgroundColor:color];

};
[self.navigationController pushViewController:vc animated:YES];

以上方法首选BLock传值,除了这些据了解还有利用单例,NSUserDefaults的方式进行传值,这会在以后进行介绍.

相关文章

  • 初步介绍iOS界面间反向传值的几种方法

    这次主要总结一下界面间的反向传值的主要方法. 1.协议传值 第二个控制器设置协议相关信息第一个控制器实现协议方法 ...

  • iOS页面间传值详解(二)

    在iOS页面间传值详解(一)中,介绍了iOS界面间的正向传值以及逆向传值的两种方法,其实逆向传值还可以使用bloc...

  • OC中反向传值的方法

    oc中反向传值四种方法 block反向传值 在需要传值的界面: 在接受到传值的界面 单例反向传值 创建一个单例类 ...

  • 几种iOS界面之间的传值方式

    几种iOS界面之间的传值方式 一.正向传值方式 (BOOL)application:(UIApplication ...

  • iOS回调,代理和block

    iOS中我们经常会遇到正反向传值,正向传值就不用多介绍了,就是属性传值(当然也可以有其他方式,这里不做介绍了). ...

  • 反向传值

    反向传值   在开发中我们经常会遇到将当前视图控制中的某些值传到另一个视图控制器,下面将介绍常用的几种反向传值的方...

  • iOS反向传值整理

    本文主要讲解iOS中常见的反向传值方法。 1.AppDelegate传值 在AppDelegate中定义相关的属性...

  • 【iOS开发细节】之- delegate代理的使用

    在iOS开发中、好多时候需要涉及到页面传值、而传值又分为正向传值和反向传值 一、 传值 1、正向传值 2、反向传值...

  • iOS中的传值方式

    在日常开发过程中,我们经常会遇到值传递。这里,介绍几种iOS开发中常见的传值方式。 1.属性传值 属性传值是iOS...

  • 界面传值

    1正向传值 2反向传值(代理) 在b界面 :1声明协议和方法(方法不实现)2定义代理属性 通知代理 a面遵守协议。...

网友评论

      本文标题:初步介绍iOS界面间反向传值的几种方法

      本文链接:https://www.haomeiwen.com/subject/qqqgjttx.html