- (void)button:(UIButton *)sender{
NSLog(@"button_click");
NSDictionary * params = @{@"class":@"CRFoundTextAController",
@"property":@{
@"ID":@"123",
@"type":@"12"
}
};
[self push:params];
}
//万能界面跳转
- (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 _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
// 检测这个对象是否存在该属性
if ([self checkIsExistPropertyWithInstance:instance verifyPropertyName:key]) {
// 利用kvc赋值
[instance setValue:obj forKey:key];
}
}];
NSLog(@"instance-%@",instance);
// 获取导航控制器
// UIWindow * window = [UIApplication sharedApplication].delegate.window;
// UITabBarController *tabVC = (UITabBarController *)window.rootViewController;
// UINavigationController *pushClassStance = (UINavigationController *)tabVC.viewControllers[tabVC.selectedIndex];
// // 跳转到对应的控制器
// [pushClassStance pushViewController:instance animated:YES];
[self.navigationController 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;
}
网友评论