溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

IOS四種反向傳值的方法

發(fā)布時間:2020-08-02 22:53:04 來源:網(wǎng)絡(luò) 閱讀:3027 作者:NetworkAD 欄目:移動開發(fā)

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

    代碼如下:(由根視圖推出子視圖,再由子視圖推出根視圖,在推出根視圖時,子視圖傳一個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];
}

方法四:代理-協(xié)議

根視圖代碼

//.h
#import <UIKit/UIKit.h>
#import "Sub1ViewController.h"
@interface ViewController : UIViewController<Sub1ViewControllerDelete>

@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 <UIKit/UIKit.h>

@protocol Sub1ViewControllerDelete <NSObject>
- (void)changeColor:(UIColor *)color;
@end

@interface Sub1ViewController : UIViewController
@property (assign,nonatomic,readwrite)id <Sub1ViewControllerDelete>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];
}


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI