iOS NSString类型转UIViewController

2022-05-18 14:45:23 浏览数 (2)

plist文件中存放着与视图控制器同名的字符串

获取工程文件:

NSString * path = [[NSBundle mainBundle] pathForResource:@"ViewControllers" ofType:@"plist"];

NSLog(@"path:%@",path);

NSArray * array = [NSArray arrayWithContentsOfFile:path];

从数组中取出:

NSDictionary * dict = [array objectAtIndex:indexPath.row];

NSString * str = [dict objectForKey:@"viewController"];

UIViewController* vc = [[NSClassFromString(str)alloc]init];

vc.title = [dict objectForKey:@"title"];

转换方法:NSString类型转换Class类型

NSClassFromString(NSString *aClassName);

NSStringFromClass(Class aClass);

//协议

NSStringFromProtocol(Protocol *proto)

NSProtocolFromString(NSString *namestr)

0 人点赞