美文网首页iOS Developer
iOS利用block逆向传值

iOS利用block逆向传值

作者: Chase_Eleven | 来源:发表于2017-07-24 15:04 被阅读0次
    传值方
    .h文件
    #import <UIKit/UIKit.h>
    
    @class AddPushCourseSearchModel;
    
    //自定义类型
    typedef void(^SelectedDiseaseBlock)(AddPushCourseSearchModel *searchModel);
    
    @interface AddPushCourseSearchViewController : UIViewController
    
    //声明自定义类型
    @property (nonatomic, copy) SelectedDiseaseBlock selectedDiseaseBlock;
    
    - (void)returnDisease:(SelectedDiseaseBlock)block;
    
    @end
    
    
    .m文件
    - (void)returnDisease:(SelectedDiseaseBlock)block{
        self.selectedDiseaseBlock = block;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //点击Cell 添加回调函数,传model
        AddPushCourseSearchModel *model = _courseList[indexPath.row];
        if (self.selectedDiseaseBlock) {
            self.selectedDiseaseBlock(model);
            [self backAction];
        }
    }
    
    接收方
    - (void)nurseAddIllnessEvent{
        AddPushCourseSearchViewController *VC = [[AddPushCourseSearchViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
        [VC returnDisease:^(AddPushCourseSearchModel *searchModel) {
            #warning 拿到model,刷新界面
        }];
    }
    

    其实可以不在.h文件暴露方法

    传值方

    .h文件
    #import <UIKit/UIKit.h>
    
    @class AddPushCourseSearchModel;
    
    //自定义类型
    typedef void(^SelectedDiseaseBlock)(AddPushCourseSearchModel *searchModel);
    
    @interface AddPushCourseSearchViewController : UIViewController
    
    //声明自定义类型
    @property (nonatomic, copy) SelectedDiseaseBlock selectedDiseaseBlock;
    
    @end
    
    
    .m文件
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       //点击Cell 添加回调函数,传model
       AddPushCourseSearchModel *model = _courseList[indexPath.row];
       if (self.selectedDiseaseBlock) {
           self.selectedDiseaseBlock(model);
           [self backAction];
       }
    }
    

    接收方

    - (void)nurseAddIllnessEvent{
        AddPushCourseSearchViewController *VC = [[AddPushCourseSearchViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
        
        VC.selectedDiseaseBlock = ^(AddPushCourseSearchModel *searchModel) {
            #warning 拿到model,刷新界面
        };
    
    }
    

    小白总结,欢迎打脸指正

    相关文章

      网友评论

        本文标题:iOS利用block逆向传值

        本文链接:https://www.haomeiwen.com/subject/uqsbkxtx.html