• 【iOS开发】- Block传值的基础学习


    前言

    • iOS传值方法多种多样,之前学习了-属性传值- KVO传值—协议传值
    • 今天了解了Block传值

    Block传值

    案例分析请添加图片描述

    • 设置2个View界面,View Second向ViewFrist传值

    VIew Second要写的

    • SecondViewController.h
    自定义一个Block属性
    • 设置按钮和函数
    // 自定义Block属性
    typedef void(^block) (NSString* secondString);
    
    @interface SecondViewController : UIViewController
    // 要传过去的字符串
    @property (nonatomic, strong)NSString* string;
    // 点击到第一界面
    @property (nonatomic, strong)UIButton* nextButton;
    
    // 定义block属性
    @property (nonatomic, copy)block myBlock;
    @end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • ** "SecondViewController.m"**
      • 点击函数里面传入我们要传过去的字符串string- _myBlock(_string);
    #import "SecondViewController.h"
    
    @interface SecondViewController ()
    
    @end
    
    @implementation SecondViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor orangeColor];
        self.title = @"有代码块的界面";
        
        // 需要传过去的字符串
        _string = [NSString stringWithFormat:@"被传过去的字符串"];
        
        _nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_nextButton setTitle:@"到达一级界面" forState:UIControlStateNormal];
        _nextButton.frame = CGRectMake(0, 0, 200, 200);
        [_nextButton addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_nextButton];
        
    }
    // 点击按钮事件的时候返回第一界面,并传值给代码块
    - (void)pressButton {
        _myBlock(_string);
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    ViewController写的

    • ViewController.h
    @interface ViewController : UIViewController
    @property (nonatomic ,strong) UILabel* label;
    @property (nonatomic, strong) UIButton* lastButton;
    
    @end
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • ViewController.m
        • 点击界面获取上一个界面传过来的string,赋值给该界面的label.text
    #import "ViewController.h"
    #import "SecondViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        _label = [[UILabel alloc] init];
        _label.font = [UIFont systemFontOfSize:21];
        _label.frame = CGRectMake(100, 300, 200, 30);
        [self.view addSubview:_label];
        
        _lastButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_lastButton setTitle:@"跳转到第二界面" forState:UIControlStateNormal];
        [_lastButton addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
        _lastButton.frame = CGRectMake(100, 400, 200, 30);
        [self.view addSubview:_lastButton];
    }
    
    - (void)pressButton {
        SecondViewController* View = [[SecondViewController alloc] init];
        // 获取上一个界面的block 传给label
        View.myBlock = ^(NSString* str) {
            self.label.text = str;
        };
        [self presentViewController:View animated:YES completion:nil];
        
        
    }
    @end
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    效果图

    请添加图片描述
    请添加图片描述
    请添加图片描述

    • 点击跳转到第二界面点击跳回然后传入string在第一界面显示即意味着完成了传值

    总结

    我认为Block传值和协议传值的目的都是底下向外传,block是在二级界面自定义代码块,然后在二级界面的点击函数里传入需要的东西,一级界面在跳转的时候接受传值即可完成。
    协议传值详解

  • 相关阅读:
    【测试】用例测试设计方法
    flink-cdc同步mysql数据到hive
    Tomcat一机多实例部署
    《Python+Kivy(App开发)从入门到实践》自学笔记:简单UX部件——Switch开关
    【ES】---Aggregation聚合,遇到String类型如何分组
    【English】joy vs joyfulness
    举个栗子~Alteryx 技巧(1):快速安装和激活 Alteryx Designer
    MachineLearning 23. 机器学习之岭回归预测基因型和表型 (Ridge)
    rabbimq之java.net.SocketException: Connection reset与MissedHeartbeatException分析
    django对比数据并调用企业微信接口群发
  • 原文地址:https://blog.csdn.net/weixin_61639290/article/details/126131602