代理
***********傳旨方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
}];