IOS四种反向传值的方法

方法一:使用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"];
}

子视图控制器代码:

//.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];

    [[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];
}

方法四:代理-协议

根视图控制器代码:

//.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文件 
@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];
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值