CHGIntent
介绍
CHGIntent将所有类视为组件,组件与组件之间的通信包括传递参数、请求码,结果处理等。组件之间实现完全解耦合。ViewController基于这种思想进行封装。特别适合多模块间的调用。
封装了打开ViewController时传递参数以及返回ViewController中需要返回值的功能。
打开一个ViewController并接收ViewController的返回值
__weak typeof(self) weakSelf = self;
CHGIntent * intent = [[CHGIntent alloc] initWithRequester:self
target:@"Sample2ViewController"
params:@"发送参数"
requestCode:1
onReceiveResultBlock:^id(id data, NSInteger requestCode, CHGIntentResultCode intentResultCode) {
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf.btn setTitle:data forState:UIControlStateNormal];
return nil;
}] ;
[intent pushViewControllerAnimated:YES];
将数据返回给客户组件
[self setResultWithData:@"测试返回数据" resultCode:CHGIntentResultCodeSuccess];
打开URL
[[CHGIntent initWithRequester:self
target:@"http://taobao.com"
params:nil
requestCode:1
onReceiveResultBlock:^id(id data, NSInteger requestCode, CHGIntentResultCode resultCode) {
return nil;
}] openAsUrl];
model向model发送请求
#import "Person.h"
#import "CHGIntent.h"
@implementation Person
/**
对狗狗说话
@param words 说话内容
*/
-(void)say2Dog:(NSString*)words {
[[[CHGIntent alloc] initWithRequester:self
target:@"Dog"
params:words
requestCode:1
onReceiveResultBlock:^id(id data, NSInteger requestCode, CHGIntentResultCode resultCode) {
NSLog(@"data:%@",data);
return nil;
}] sendRequest];
}
@end
#import "Dog.h"
@implementation Dog
- (instancetype)init
{
self = [super init];
if (self) {
//模拟返回数据给调用组件 3秒后返回结果
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self waggedHisTail];
});
}
return self;
}
-(void)waggedHisTail {
[self setResultWithData:@"我来了" resultCode:CHGIntentResultCodeSuccess];
[self close];//如果不再需要向组件回消息 需要关闭
}
@end