I recently needed to dynamically create UIViewController objects based on class/nib names defined in a configuration file, then display them in a UINavigationController.
Here’s how it’s done:
// Dynamically create new view controller based on class name string, then add it to
// my app via [UINavigationController setViewControllers:animated:]
NSString *myUIViewControllerClassName = @"MyViewController";
Class myClass = NSClassFromString(myUIViewControllerClassName);
NSObject *myObject = [myClass new];
if( [myObject isKindOfClass:[UIViewController class]] )
{
UIViewController *newViewController = (UIViewController *) myObject;
[newViewController initWithNibName:myUIViewControllerClassName bundle:nil];
[appDelete.navigationController setViewControllers:[NSArray arrayWithObject:newViewController] animated:NO];
[newViewController release];
}
else
{
[myObject release];
}