Block 代理

作者: 其实也没有 | 来源:发表于2017-04-10 20:37 被阅读25次

    代理
    ***********传旨方3步(定义协议 传递参数)****************
    1)定义协议.方法
    2)声明一个属性,记录代理对象
    3)在合适的时机,给代理方发消息

    .h
    1,@protocol PaperClickDelegate <NSObject>
    -(void)BtnClick:(PaperCellModel*)viewModel;
    @end
    2,@property(nonatomic,assign)id delegate;
    .m
    3,[self.delegate BtnClick:self.paperCellModel];
    

    ************接旨方3步(遵守协议 实现方法)***************
    1)遵守协议
    2)实现方法
    3)设置代理

    .h
    1, @interface PaperTestTableViewController() <PaperClickDelegate>
    .m
    2, cell.delegate=self;
    3,-(void)BtnClick:(PaperCellModel*)viewModel{
    [viewModel.paperModel.userMeta.currentPracticeId integerValue]
    }
    

    Block
    Block深究浅析(上篇)-Block本质
    Block深究浅析(中篇)-内存管理与变量传递
    Block深究浅析(下篇)-开发中使用场景
    Objective-C中的Block

    ************传值回调(逆向传值)***************

    场景:控制器的逆传实现方式很多,这里讲一下modal方式的block逆传.A控制器modal出B控制器,B控制器dismiss后传值给A控制器

    1:声明一个带参数Block属性
    2:在需要传值控制器中定义Block
    3:在传值控制器中调用Block

    ***控制器B:  (声明block,传值)***
    // ModalVC.h (1)  <也可以是view>
    // 在要modal的控制器B声明一个带参数block属性
    @property (nonatomic ,strong) void(^valueBlcok)(int value);
    //@property(nonatomic,copy)void(^cellClickBlock)(NSString*msg);
    
    // ModalVC.m   (3)
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        // 传值
        if (_valueBlock) {
            _valueBlock(@"123");
        }
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
       // 点击事件触发block传值
    //self.cellClickBlock(model.paperModel.ID);
    
    
    -----------------------
    ***控制器A:  (2) (接收 处理后 的数据)***
    2,
    //回掉接值方
    /*
    .m
    VC.cellClickBlock= ^(NSString*msg) {
    DLog(@"点击的ID==%@",msg);
    };
    */
    
    // viewContent.m  (控制器获取点击view事件)
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        ModalViewController *modalVc = [[ModalViewController alloc] init];
     
        modalVc.valueBlock = ^(NSString *value){
          // 回调
            NSLog(@"%@",value);
            
        };
        
        [self presentViewController:modalVc animated:YES completion:nil];
    }
    

    **************Block 请求回调*******************
    B:

    NetworkRequest.h
    /**
     *  成功。
     *  @param responseObject 返回的数据。
     */
    typedef void(^Succsess)(id responseObject);
    
    /**
     *  失败。
     *  @param error 失败原因。
     */
    typedef void(^Failure)(NSString *errorMessage);
    
    +(void)requestWithParameter:(NSDictionary *)dic withSuccess:(Succsess)succsess andFailure:(Failure)failure;
    
    
    NetworkRequset.m
    
    +(void)requestWithParameter:(NSDictionary *)dic withSuccess:(Succsess)succsess andFailure:(Failure)failure{
    
    
       if(succsess){
                    succsess(@{@"result":[NSNumber numberWithBool:YES],@"modelarray":mArray,@"myRankModel":myRank});
      }
       if(failure){
                DLog(@"error");
                failure([request.ErrorDic objectForKey:@"message"]);
         }
    
    
    }
    
    
    
    ----------
    
    
    HTIAPHelper.h
    
    typedef void(^buyCompletionBlock)(NSString *identifier);
    typedef void(^restoreCompletionBlock)(NSArray *products);
    typedef void(^failedBlock)(NSString *reason);
    
    // 回调块代码
    @property (nonatomic, copy) buyCompletionBlock      buyCompletion;
    @property (nonatomic, copy) restoreCompletionBlock  restoreCompletion;
    @property (nonatomic, copy) failedBlock             failedBlock;
    
    
    HTIAPHelper.m
    - (void)buyProduct:(NSString *)identifier
            completion:(buyCompletionBlock)completion
                failed:(failedBlock)failed
    {
        // 记录回调块代码
        self.buyCompletion = completion;
        self.failedBlock = failed;
    }
    
    
    // 回调传值
    #warning -Break Cycle 是否要打破循环?
    //注意:你可以直接在 block 回调中使用 self,不用担心循环引用。因为 YTKRequest 会在执行完 block 回调之后,将相应的 block 设置成 nil。从而打破循环引用。
       //  __weak typeof(self) wself = self;
        [requset startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest *request) {
            NSInteger code = [[request.responseJSONObject objectForKey:@"code"] integerValue];
            if (code== 1000000) {
             //      __strong typeof(wself) sself = wself;
                self.buyCompletion(@"1000000");
           //       [sself->_buyCompletion addObject:@"1000000"];           
            }else
            {
             //     __strong typeof(wself) sself = wself;
                self.buyCompletion(@"-1");
           //       [sself->_buyCompletion addObject:@"-1"];
            }
            
            
        } failure:^(__kindof YTKBaseRequest *request) {
        if (_failedBlock) {
                _failedBlock([request.userInfo objectForKey:@"message"]);
            }
        }];
    
    
    
    

    A:

        [[HTIAPHelper sharedIAPHelper] buyProduct:_Salemodel.inPurchaseProductId completion:^(NSString *identifier) {
            //identifier = self.buyCompletion(@"");
            if ([identifier isEqualToString:@"1000000"]) {
        //do SomeThings
    }
        } failed:^(NSString *reason) {
    //reason==  _failedBlock()
        //do SomeThings
        }];
    

    相关文章

      网友评论

        本文标题:Block 代理

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