• 【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是在二级界面自定义代码块,然后在二级界面的点击函数里传入需要的东西,一级界面在跳转的时候接受传值即可完成。
    协议传值详解

  • 相关阅读:
    FinClip 支持创建 H5应用类小程序;PC 终端 优化升级
    PWN利器-pwntools安装、调试教程一览
    家政类小程序开发,互联网+家政系统,全套家政系统开发方案
    Nodejs 应用编译构建提速建议
    后端——egg.js是什么、egg.js安装、约定规则、路由Router、控制器Controller、跨域
    WebRTC系列-网络传输之7-ICE补充之提名(nomination)与ICE_Model
    设计模式:外观模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)
    高通camera之对camx架构的浅析
    CANoe制作网关实现CAN(FD)报文故障注入(报文长度/timeout/信号错误/E2E)1
    UWB安全数据通讯STS-加密、身份认证
  • 原文地址:https://blog.csdn.net/weixin_61639290/article/details/126131602