服务端控制跳转控制器

服务端控制跳转控制器

思路

  1. 服务端返回跳转控制器相应的类名字符串
  2. 根据类名字符串找到相应的类,如果没有则动态的创建相应的类
  3. 创建相应的实例,传值
  4. 获取当前的导航控制器,并跳转

测试数据

 NSDictionary *userInfo = @{@"class": @"YSViewController",
                           @"property": @{
                                   @"ID": @"124556",
                                   @"type": @"1"
                                   }
                           };

获取相应的类

/**
 *  跳转界面
 */
- (void)push:(NSDictionary *)params
{
// 类名
NSString *class =[NSString stringWithFormat:@"%@", params[@"class"]];
const char *className = [class cStringUsingEncoding:NSASCIIStringEncoding];

// 从一个字串返回一个类
Class newClass = objc_getClass(className);
if (!newClass)
{
    // 创建一个类
    Class superClass = [NSObject class];
    newClass = objc_allocateClassPair(superClass, className, 0);
    // 注册你创建的这个类
    objc_registerClassPair(newClass);
}
// 创建对象
id instance = [[newClass alloc] init];

NSDictionary *propertys = params[@"property"];
[propertys enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    // 检测这个对象是否存在该属性
    if ([self checkIsExistPropertyWithInstance:instance verifyPropertyName:key]) {
        // 利用kvc赋值
        [instance setValue:obj forKey:key];
    }
}];


// 获取导航控制器
UITabBarController *tabVC = (UITabBarController *)self.window.rootViewController;
UINavigationController *pushClassStance = (UINavigationController *)tabVC.viewControllers[tabVC.selectedIndex];

// 跳转到对应的控制器
[pushClassStance pushViewController:instance animated:YES];
}

判断该类是否有相应的属性

- (BOOL)checkIsExistPropertyWithInstance:(id)instance verifyPropertyName:(NSString *)verifyPropertyName

{
unsigned int outCount, i;

// 获取对象里的属性列表
objc_property_t * properties = class_copyPropertyList([instance
                                                       class], &outCount);

for (i = 0; i < outCount; i++) {
    objc_property_t property =properties[i];
    //  属性名转成字符串
    NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
    // 判断该属性是否存在
    if ([propertyName isEqualToString:verifyPropertyName]) {
        free(properties);
        return YES;
    }
}
free(properties);

return NO;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,187评论 6 13
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,814评论 0 9
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,010评论 19 139
  • 在项目中,我们可能碰到这样的情况,产品想要一个灵活的入口。比如首页的轮播banner、商品中间穿插的banner等...
    皮蛋solo粥阅读 580评论 1 4
  • 前段时间外婆去世,之后一段时间到现在,差不多一个月了,我的状态就没有好过。在她还在世的时候,我为她马上离世而难过了...
    夏绿地阅读 527评论 2 1