IOS四種反向傳值的方法

方法一:使用target-action設(shè)計(jì)模式

代碼如下:(由根視圖推出子視圖,再由子視圖推出根視圖,在推出根視圖時(shí),子視圖傳一個(gè)color的屬性給根視圖,用來(lái)修改根視圖的背景顏色)

根視圖控制器代碼:

//.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:@"進(jìn)入下一個(gè)視圖控制器"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:@"進(jìn)入根視圖控制器"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:@"進(jìn)入下一個(gè)視圖控制器"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:@"進(jìn)入根視圖控制器"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:@"進(jìn)入下一個(gè)視圖控制器"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:@"進(jìn)入根視圖控制器"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];

}

方法四:代理-協(xié)議
根視圖代碼

//.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:@"進(jìn)入下一個(gè)視圖控制器"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:@"進(jìn)入根視圖控制器"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];

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容