从字符串构建类名

时间:2011-09-12 16:40:46

标签: iphone objective-c ios class object

我有一个像这样的字符串数组:

tableArray: { "Map", "Web", "Help" + 10 other strings}

该数组用于在UITableViewController中构建表格单元格。点击时我希望在didSelectRowAtIndexPath方法中显示相应的视图控制器。

而不是创建一个大型开关,或者如果.. then .. else语句,我可以以某种方式从上面的字符串创建类名,以便Map成为MapViewController:

MapViewController *detailViewController = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

..和“Web”创建WebViewController等。

1 个答案:

答案 0 :(得分:1)

Class theClass = NSClassFromString(classNameStr);
id myObject = [[theClass alloc] init];