构建类名称表单字符串 [英] Building class names form strings

查看:194
本文介绍了构建类名称表单字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an array of strings like this:

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

该数组用于在UITableViewController中构建表单元格。当你点击我想要相应的视图控制器显示,在didSelectRowAtIndexPath方法。

The array is used to build the table cells in a UITableViewController. When tapped I want the appropriate view controller to show, in the didSelectRowAtIndexPath method.

而不是创建一个大的开关,或如果.. then .. else语句,不知何故从上面的字符串创建类名,使Map成为MapViewController:

Instead of creating a large switch, or if .. then .. else statement, can I somehow create the class name from the strings above, so that Map becomes MapViewController:

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

..和Web创建WebViewController等。

.. and "Web" creates WebViewController etc.

推荐答案

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

这篇关于构建类名称表单字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆