user1573607
user1573607

Reputation: 522

IOS - Load unknown type of UIViewController

I'm trying to load UIViewController, but not knowing the type until runtime.

So I have some different UIViewControllers, each one with it's own .xib, and with the following code, the .xib is showed correctly, but the UIViewController is never created, because it's a child of UIViewController, not an exactly UIViewController:

UIViewController *vc = [[UIViewController alloc] initWithNibName:[[self component] xibName] bundle:nil];
[self.navigationController pushViewController:vc animated:YES];

It works fine for example in this particular case, but obviously not for the rest:

Controller1 *vc = [[Controller1 alloc] initWithNibName:[[self component] xibName] bundle:nil];
[self.navigationController pushViewController:vc animated:YES];

How can I solve it? Thanks!

Upvotes: 0

Views: 383

Answers (1)

touti
touti

Reputation: 1164

you can put your controller classe name in an NSSTring and create the object like this

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

Upvotes: 1

Related Questions