Objective c IOS-加载未知类型的UIViewController

Objective c IOS-加载未知类型的UIViewController,objective-c,ios,uiviewcontroller,runtime,Objective C,Ios,Uiviewcontroller,Runtime,我正在尝试加载UIViewController,但直到运行时才知道其类型 所以我有一些不同的UIViewController,每个都有自己的.xib和 下面的代码正确显示了.xib,但从未创建UIViewController,因为 它是UIViewController的子级,而不是UIViewController: UIViewController *vc = [[UIViewController alloc] initWithNibName:[[self component] xibName]

我正在尝试加载UIViewController,但直到运行时才知道其类型

所以我有一些不同的UIViewController,每个都有自己的.xib和 下面的代码正确显示了.xib,但从未创建UIViewController,因为 它是UIViewController的子级,而不是UIViewController:

UIViewController *vc = [[UIViewController alloc] initWithNibName:[[self component] xibName] bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
例如,在这种特殊情况下,它可以正常工作,但在其他情况下显然不行:

Controller1 *vc = [[Controller1 alloc] initWithNibName:[[self component] xibName] bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
我怎样才能解决它?
谢谢

您可以将控制器类名称放入NSSTring中,并创建如下对象

UIViewController*vc=[[NSClassFromString(ControllerclassName)alloc]init];
[self.navigationController pushViewController:vc动画:是];

UIViewController *vc=[[NSClassFromString(ControllerclassName) alloc] init]; [self.navigationController pushViewController:vc animated:YES];