iOS之block

block传值:

使用背景:一个消息聊天界面 ChatVC,一个对具体消息的长按事件所对应的LongPressVC。


1.在LongPressVC.h中:

typedef void (^DeleteMessageHandle)(MessageModel * message);

typedef void (^ReSendMessageHandle)(MessageModel * message);

typedef void (^ReRecvMessageHandle)(MessageModel * message);


@property (copy  , nonatomic) DeleteMessageHandle deleteMessage;

@property (copy  , nonatomic) ReSendMessageHandle reSendMessage;

@property (copy  , nonatomic) ReRecvMessageHandle reRecvMessage;


此处为自定义代码块。



2.在ChatVC.m中:

LongPressVC * longPVC = [[LongPressVCalloc] init];

longPVC.deleteMessage = ^(MessageModel * message){

            [self deleteReloadRowWith:message];

        };

        longPVC.reSendMessage = ^(MessageModel * message) {

            if (message.type ==TYPE_SEND_MESSAGE

                || message.type == TYPE_RECV_MESSAGE) {

                [self sendMessageModel:message.content_txt];

            } else {

                if ([LocalStorageisExistFileWithFilePath:[LocalStoragegetDownloadPath] fileName:message.content_txt]) {

                    [selfsendFileName:message.content_txt FilePath:[LocalStorage getDownloadPath]];

                } else {

                    ERRORLog(@"文件不存在");

                }

            }

        };

        longPVC.reRecvMessage = ^(MessageModel * message) {

            dispatch_async(dispatch_get_main_queue(), ^{

                [self receiveFilesWithMessage:message];

            });

        };


此处为获取从LongPressVC.m 中传过来的消息,并做相应处理。



3.当需要处理该消息时,从LongPressVC.m 中将该值传入。   使ChatVC.m中写入的代码块来处理这个消息体。

self.deleteMessage(self.messageModel);

self.deleteMessage(self.messageModel);

等等。



为使LongPressVC中的数据可以传到ChatVC中处理,我们在LongPressVC中自定义相关的代码块,并在适当的时候将值传入代码块中。在ChatVC方面,我们只需要alloc出LongPressVC的对象,并收取LongPressVC中的对象,并做处理即可。

 longPVC.reRecvMessage = ^(MessageModel * message) {

// 此处做相应处理动作

            dispatch_async(dispatch_get_main_queue(), ^{

                [self receiveFilesWithMessage:message];

            });

        };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值