block页面之间简单传值

本文介绍了一个简单的iOS应用案例,展示了如何使用Swift通过block在两个视图控制器间传递数据。具体实现包括从一个视图控制器跳转到另一个视图控制器,并在返回时将数据带回到初始视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看一下效果图:


//
//  ViewController.h
//  Block
//
//  Created by LiuMingchuan on 15/9/29.
//  Copyright © 2015年 LMC. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (IBAction)btnToSecond:(id)sender;

@property (strong, nonatomic) IBOutlet UILabel *infoLabel;


@end
//
//  ViewController.m
//  Block
//
//  Created by LiuMingchuan on 15/9/29.
//  Copyright © 2015年 LMC. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.infoLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.infoLabel.numberOfLines = 0;
    // Do any additional setup after loading the view, typically from a nib.
}

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

- (IBAction)btnToSecond:(id)sender {
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    SecondViewController *sec = [storyBoard instantiateViewControllerWithIdentifier:@"second"];
    SetInfoBlock setInfoBlock = ^(NSString *info){//创建SetInfoBlock类型的block
        self.infoLabel.text = info;//block体的作用就是将传入block的文字列赋值给我们第一个视图的UILabel的text属性
    };
    //设定secondViewController的block
    [sec setInfo:setInfoBlock];
    [sec setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentViewController:sec animated:YES completion:nil];

}
@end


//
//  SecondViewController.h
//  Block
//
//  Created by LiuMingchuan on 15/9/30.
//  Copyright © 2015年 LMC. All rights reserved.
//

#import <UIKit/UIKit.h>
/**
 *  Block的定义
 *
 *  @param info 文字列形参
 */
typedef void(^SetInfoBlock)(NSString *info);

@interface SecondViewController : UIViewController

- (IBAction)backToOneBtn:(id)sender;

@property (strong, nonatomic) IBOutlet UITextView *inputText;

/**
 *  自定义block的声明
 */
@property (nonatomic,copy) SetInfoBlock setInfoBlock;

/**
 *  用来设定本Controller的block
 *
 *  @param setInfoBlock <#setInfoBlock description#>
 */
- (void)setInfo:(SetInfoBlock)setInfoBlock;

@end
//
//  SecondViewController.m
//  Block
//
//  Created by LiuMingchuan on 15/9/30.
//  Copyright © 2015年 LMC. All rights reserved.
//

#import "SecondViewController.h"
#import "ViewController.h"

@implementation SecondViewController

- (IBAction)backToOneBtn:(id)sender {

    //当我们的block不为nil时进行处理
    if (self.setInfoBlock != nil) {
        //将我们输入的内容传入从第一个视图传进来的block中
        self.setInfoBlock(self.inputText.text);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
/**
 *  接受外部传入的block
 *
 *  @param setInfoBlock 传入的block
 */
- (void)setInfo:(SetInfoBlock)setInfoBlock{
    self.setInfoBlock = setInfoBlock;
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值