zoukankan      html  css  js  c++  java
  • iOS-runtime-根据类名推送到任意控制器,且实现属性传值

    //
    //  WJRuntime.m
    //  RuntimeSkip
    //
    //  Created by tqh on 15/9/8.
    //  Copyright (c) 2015年 tqh. All rights reserved.
    //
    
    #import "WJRuntime.h"
    
    #import <objc/runtime.h>
    @implementation WJRuntime
    
    //runtime跳转
    
    + (void)runtimePush:(NSString *)vcName dic:(NSDictionary *)dic nav:(UINavigationController *)nav {
        //类名(对象名)
        
        NSString *class = vcName;
        
        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];
        
        //下面是传值--------------
        
        [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            if ([self checkIsExistPropertyWithInstance:instance verifyPropertyName:key]) {
                //kvc给属性赋值
                
                [instance setValue:obj forKey:key];
            }else {
                NSLog(@"不包含key=%@的属性",key);
            }
        }];
        [nav 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;
    }
    
    @end
     
  • 相关阅读:
    Centos 7.0 下安装 Zabbix server 3.0服务器的安装及 监控主机的加入(1)
    Linux系统级别能够打开的文件句柄的数file-max命令
    记:cloudstack--gluster主存储上的一个文件损坏导致SSVM启动失败
    Linux 之 inotify+rsync 备份文件系统
    为什么KVM计算机点无故重启?
    vim批量在文件每行添加内容以及查询cloudstack模板是否是增量
    记-cloudstack 更改二级存储
    apache 自定义404错误页面
    URL路由
    前端图片优化
  • 原文地址:https://www.cnblogs.com/hxwj/p/4793363.html
Copyright © 2011-2022 走看看