iOS反向传值--Block方法

RootViewController代码如下:

#import "RootViewController.h"
#import "MyControl.h"
#import "SecondViewController.h"

#define kDebugPrint NSLog(@"%s",__func__)
@interface RootViewController ()
{
    UILabel *_label;
}
@end

@implementation RootViewController

/*
 正向传值
 创建第一个界面   通过第一个界面跳转到第二个界面
 如果由第一个界面向第二个界面 进行传值 正向传值
 属性传值
 
 
 第二张向第一张界面传值 反向传值
 
 下级界面向上一级界面传值---》反向传值
 
 反向传值方式:
 
 1.代理传值
 下级界面要把textField的内容 传给 上一级,这时下级界面就可以委托上级界面 修改 label的值
 第二个界面(主动方) 可以制定一个协议 规范代理的行为,
 
 第一个界面(被动方) 遵守协议 作为 代理
 
 
 
 
 2.单例传值 1.系统单例 2.自定义单例
 
 3.通知传值
 
 4.NSUserDefaults
 
 5.block传值
 
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showUI];
}
- (void)showUI {
    self.view.backgroundColor = [UIColor grayColor];
    
    _label = [MyControl creatLabelWithFrame:CGRectMake(0, 30, 300, 30) text:@"XXX"];
    _label.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_label];
    
    UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 200, 300, 50) target:self sel:@selector(btnClick:) tag:201 image:nil title:@"切换到第二张"];
    [self.view addSubview:button];
}

- (void)btnClick:(UIButton *)btn {
    //每次点击按钮  都会创建一个新的第二张对象
    SecondViewController *svc = [[SecondViewController alloc] init];
    
    [svc setMyBlock:^(NSString *textStr) {
        _label.text=textStr;
    }];
    
    [self presentViewController:svc animated:YES completion:nil];
    [svc release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

SecondViewController点h文件声明如下代码:

#import <UIKit/UIKit.h>
//-----------
typedef void(^ChangeTextBlock)(NSString *textStr);

@interface SecondViewController : UIViewController
{
    //void(^myBlock)(NSString *textStr);
    ChangeTextBlock _myBlock;

}
//修改和获取
-(void)setMyBlock:(ChangeTextBlock)block;
-(ChangeTextBlock) myBlock;
@end
#import "SecondViewController.h"
#import "MyControl.h"
#define kDebugPrint NSLog(@"%s",__func__)

@interface SecondViewController ()
{
    UITextField *_textField;
}
@end

@implementation SecondViewController
- (void)dealloc {
    kDebugPrint;
    [_myBlock release];
    [super dealloc];
}

-(void)setMyBlock:(ChangeTextBlock)block{
    if (_myBlock!=block) {
        [_myBlock release];
        _myBlock=[block copy];//拷贝block
    }

}
-(ChangeTextBlock) myBlock{
    return _myBlock;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    [self showUI];
}

- (void)showUI {
    UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 30, 300, 30) target:self sel:@selector(btnClick:) tag:301 image:nil title:@"返回"];
    [self.view addSubview:button];
    
    UIButton *button2 = [MyControl creatButtonWithFrame:CGRectMake(10,200 , 300, 30) target:self sel:@selector(btnClick2:) tag:302 image:nil title:@"传值"];
    [self.view addSubview:button2];
    
    
    _textField = [MyControl creatTextFieldWithFrame:CGRectMake(10, 100, 300, 30) placeHolder:nil delegate:nil tag:100];
    [self.view addSubview:_textField];
}
//收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_textField resignFirstResponder];
}
- (void)btnClick:(UIButton *)btn {
    //返回上一级
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)btnClick2:(UIButton *)btn {
    //点击传值  去委托block执行修改第一个界面label的值
    
    if (self.myBlock) {
        //_myBlock(_textField.text);
        //等价于上一句
        self.myBlock(_textField.text);
    }else{
        NSLog(@"没有传入block");
    
    }
}
@end


转载于:https://my.oschina.net/u/2410306/blog/523969

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值