iphone - 构建类名形成字符串

标签 iphone objective-c ios class object

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

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/

相关文章:

iphone - 为什么我的 iPhone 应用程序在后台继续运行?

Java:如何处理可能需要大约 10 秒的 API 调用

ios - iOS8 中的多行标签

ios - 如果一个版本已经准备好出售,请将二进制文件上传到应用商店?

ios - 添加 View Controller 作为启动屏幕

ios - 在 iOS 中正确使用 View Controller 和 MVC 模式

objective-c - Cocoa NSRect 位于交叉点之外

ios - 如何让 Sprite 响应接触事件而不是碰撞事件(SpriteKit)?

ios - 更改 iOS SwiftUI 键盘扩展高度

iphone - 处理 iPhone 中已弃用的方法