我有一个像这样的字符串数组:
tableArray: { "Map", "Web", "Help" + 10 other strings}
该数组用于在 UITableViewController 中构建表格单元格。点击时,我希望在 didSelectRowAtIndexPath 方法中显示适当的 View Controller 。
我可以不创建一个大的开关,或者 if .. then .. else 语句,而是以某种方式从上面的字符串创建类名,以便 Map 成为 MapViewController:
MapViewController *detailViewController = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
.. 和“Web”创建 WebViewController 等。
最佳答案
Class theClass = NSClassFromString(classNameStr);
id myObject = [[theClass alloc] init];
关于iphone - 构建类名形成字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7391152/