标签:char line family round color get classname art pop
利用runtime我们能够动态生成对象、属性、方法这特性
假定我们要动态生成DYViewController,并为它创建属性propertyName
1)对象名
NSString *class = @"DYViewController";
const char *className = [class cStringUsingEncoding:NSASCIIStringEncoding];
2)从一个字符串返回一个Class
Class newClass = objc_getClass(className);
3)创建类
Class superClass = [UIViewController class];
newClass = objc_allocateClassPair(superClass, className, 0);
4) 注冊
注冊你创建的这个类
objc_registerClassPair(newClass);
5)创建DYViewController对象id instance = [[newClass alloc] init];
6)给类添加属性propertyName,值为propertyValue
利用kvc赋值
[instance setValue:propertyValue forKey:propertyName];
标签:char line family round color get classname art pop
原文地址:http://www.cnblogs.com/yxysuanfa/p/6991072.html