gpt4 book ai didi

iphone - 构建类名形成字符串

转载 作者:行者123 更新时间:2023-11-29 04:58:54 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com