美文网首页
IOS四种反向传值的方法

IOS四种反向传值的方法

作者: fe4a5edc73de | 来源:发表于2016-09-28 12:38 被阅读68次

方法一:使用target-action设计模式

代码如下:(由根视图推出子视图,再由子视图推出根视图,在推出根视图时,子视图传一个color的属性给根视图,用来修改根视图的背景颜色)

根视图控制器代码:

//.m文件

- (void)viewDidLoad

{

[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];

[self createButton];

}

- (void)createButton

{

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(10, 30, 300, 40);

[btn setTitle:@"进入下一个视图控制器"forState:UIControlStateNormal];

btn.layer.cornerRadius = 5;

btn.backgroundColor = [UIColor blackColor];

[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

- (void)btnClick

{

Sub1ViewController * sub1 = [[Sub1ViewController alloc]init];

sub1.view.backgroundColor = [UIColor blueColor];

sub1.target = self;

sub1.action = @selector(changeColor:);

[self presentViewController:sub1 animated:YES completion:nil];

}

- (void)changeColor:(UIColor *)color

{

self.view.backgroundColor = color;

}

子视图代码:

//.h文件

@interface Sub1ViewController : UIViewController

@property (assign,readwrite,nonatomic)id target;

@property (assign,readwrite,nonatomic)SEL action;

@end

//.m文件

- (void)viewDidLoad

{

[super viewDidLoad];

[self createPopToRootViewBtn];

}

- (void)createPopToRootViewBtn

{

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(10, 30, 300, 40);

[btn setTitle:@"进入根视图控制器"forState:UIControlStateNormal];

btn.layer.cornerRadius = 5;

btn.backgroundColor = [UIColor blackColor];

[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

- (void)btnClick

{

if([_target respondsToSelector:_action])

{

[_target performSelector:_action withObject:[UIColor orangeColor]];

}

[self dismissViewControllerAnimated:YES completion:nil];

}

方法二:通知

//.m根视图

- (void)viewDidLoad

{

[super viewDidLoad];

self.view.backgroundColor = [UIColor redColor];

[self createButton];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"ChangeColor"object:nil];

}

- (void)createButton

{

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(10, 30, 300, 40);

[btn setTitle:@"进入下一个视图控制器"forState:UIControlStateNormal];

btn.layer.cornerRadius = 5;

btn.backgroundColor = [UIColor blackColor];

[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

- (void)btnClick

{

Sub1ViewController * sub1 = [[Sub1ViewController alloc]init];

sub1.view.backgroundColor = [UIColor blueColor];

[self presentViewController:sub1 animated:YES completion:nil];

}

- (void)changeColor:(NSNotification *)nofi

{

self.view.backgroundColor = [nofi.userInfo objectForKey:@"color"];

}

子视图代码

- (void)viewDidLoad

{

[super viewDidLoad];

[self createPopToRootViewBtn];

}

- (void)createPopToRootViewBtn

{

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(10, 30, 300, 40);

[btn setTitle:@"进入根视图控制器"forState:UIControlStateNormal];

btn.layer.cornerRadius = 5;

btn.backgroundColor = [UIColor blackColor];

[[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeColor"object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor],@"color", nil]];

[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

- (void)btnClick

{

[self dismissViewControllerAnimated:YES completion:nil];

}

方法三:代码块

根视图代码:

- (void)viewDidLoad

{

[super viewDidLoad];

self.view.backgroundColor = [UIColor redColor];

[self createButton];

}

- (void)createButton

{

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(10, 30, 300, 40);

[btn setTitle:@"进入下一个视图控制器"forState:UIControlStateNormal];

btn.layer.cornerRadius = 5;

btn.backgroundColor = [UIColor blackColor];

[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

- (void)btnClick

{

Sub1ViewController * sub1 = [[Sub1ViewController alloc]init];

sub1.view.backgroundColor = [UIColor blueColor];

sub1.MyBlock = ^(UIColor * color)

{

self.view.backgroundColor = color;

};

[self presentViewController:sub1 animated:YES completion:nil];

子视图代码

//.h文件

@interface Sub1ViewController : UIViewController

@property (copy,nonatomic,readwrite)void(^MyBlock)(UIColor * color);

@end

//.m文件

- (void)viewDidLoad

{

[super viewDidLoad];

[self createPopToRootViewBtn];

}

- (void)createPopToRootViewBtn

{

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(10, 30, 300, 40);

[btn setTitle:@"进入根视图控制器"forState:UIControlStateNormal];

btn.layer.cornerRadius = 5;

btn.backgroundColor = [UIColor blackColor];

[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

- (void)btnClick

{

_MyBlock([UIColor orangeColor]);

[self dismissViewControllerAnimated:YES completion:nil];

}

方法四:代理-协议
根视图代码

//.h

#import 

#import "Sub1ViewController.h"

@interface ViewController : UIViewController

@end

//.m

- (void)viewDidLoad

{

[super viewDidLoad];

self.view.backgroundColor = [UIColor redColor];

[self createButton];

}

- (void)createButton

{

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(10, 30, 300, 40);

[btn setTitle:@"进入下一个视图控制器"forState:UIControlStateNormal];

btn.layer.cornerRadius = 5;

btn.backgroundColor = [UIColor blackColor];

[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

- (void)btnClick

{

Sub1ViewController * sub1 = [[Sub1ViewController alloc]init];

sub1.view.backgroundColor = [UIColor blueColor];

sub1.delegate = self;

[self presentViewController:sub1 animated:YES completion:nil];

}

- (void)changeColor:(UIColor *)color

{

self.view.backgroundColor = color;

}

子视图控制器

//.h文件

#import 

@protocol Sub1ViewControllerDelete 

- (void)changeColor:(UIColor *)color;

@end

@interface Sub1ViewController : UIViewController

@property (assign,nonatomic,readwrite)id delegate;

@end

//.m文件

- (void)viewDidLoad

{

[super viewDidLoad];

[self createPopToRootViewBtn];

}

- (void)createPopToRootViewBtn

{

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(10, 30, 300, 40);

[btn setTitle:@"进入根视图控制器"forState:UIControlStateNormal];

btn.layer.cornerRadius = 5;

btn.backgroundColor = [UIColor blackColor];

[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

- (void)btnClick

{

[_delegate changeColor:[UIColor orangeColor]];

[self dismissViewControllerAnimated:YES completion:nil];

}

相关文章

  • OC中反向传值的方法

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

  • iOS反向传值整理

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

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

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

  • IOS四种反向传值的方法

    方法一:使用target-action设计模式 代码如下:(由根视图推出子视图,再由子视图推出根视图,在推出根视图...

  • ioS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • iOS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • Block传值

    iOS传值一共有四种:属性传值,代理传值,通知传值以及Block传值; 今天我们来说一下Block传值: 概念:带...

  • iOS (反向)传值

    代理、block、消息中心、单例。 正向传值 通过属性(特性)的值,在上个使用本类(所在类)对象的类中,直接传递其...

  • ios反向传值

    方法三:代码块 根视图代码: //.m文件 -(void)viewDidLoad { [superviewDidL...

  • iOS反向传值

    常用的方式 代理 block 通知 也可以使用以下方式但不推荐 单例 全局变量 NSUserDefaults 文件...

网友评论

      本文标题:IOS四种反向传值的方法

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