First, move the name of the NIB into the ViewController class with code such as:
- (MoreViewController *)init
{
self = [super initWithNibName:@"MoreView" bundle:nil];
if (self != nil)
{
}
return self;
}
This will make your view controllers self-contained so you can create them cleanly:
MoreViewController *moreViewController = [[MoreViewController alloc] init];
I recommend this approach no matter what else you do. Apple's sample code not withstanding, it is silly to put the name of the NIB in the caller. Why should every caller to a view controller need to know this internal implementation detail?
Next, remember that ObjC is a dynamic language. You can construct objects based on their Class name like this:
Class vcClass = NSClassFromString([self.classNameForSender objectForKey:sender])
UIViewController *vc = [[[vcClass alloc] init] autorelease];
[self switchToView:vc];
In the above, -classNameForSender
is an NSDictionary
that maps the button objects to the class name, but you can get these class names lots of other ways of course. Depending on the sender, you may be able to hang the class directly on the sender (though this is difficult with standard UIControl
s because they only have an integer tag
to work with).
I haven't duplicated your storing the VC into an ivar. Do you really need this? If so, you can store it in a dictionary by name or you can store all the VCs in an NSMutableSet
. But in most cases you really don't need this since most UI controllers (like NavController) will manage memory for you.
Another less dynamic approach is to create all the view controls at the beginning and put them in a dictionary, then you can just load them out of the dictionary based on the sender. This has the advantage of better compile-time checks, but can eat a lot more memory.