Initialize custom class according to user selection
I have a navigation based application with two levels开发者_JS百科, in the second level the user select an option which should cause initialization and loading of the proper Nib file (there is a Nib file for every available selection).
Now I'm doing the initialization in a switch, based on the user selection. The problem is that I'm adding Nibs as I going through the development, and now I need to update the switch part for every Nib I add.
I would like to create a *.plist file with the Nib names and to load the Nib according to a value in the plist file, I didn't managed to create a class with a value in a nsstring variable.
Here is one of my tries- Nib name is nsstring with the nib name value.
[code]
childController = [[Nibname alloc] initWithNibName:@Nibname bundle:nil];
[/code]
any help will be appreciated
Thx
Since we're talking about pushing view controllers here, you could generalise your 'alloc', and use the nibname to load the correct nib. Like this:
// load info from your plist into a dictionary
NSString * path= [[NSBundle mainBundle] pathForResource:@"AllNibs" ofType:@"plist"];
NSDictionary * nibs = [NSDictionary dictionaryWithContentsOfFile:path];
// use a value to select the right nib name
NSString *selector = @"viewController1";
NSString *nibName = [nibs objectForKey:selector];
// create the view controller from the selected nib name
UIViewController *aController = [[UIViewController alloc] initWithNibName:nibName bundle:nil];
Even if you've subclassed UIViewController, this will still load the correct class from the nib name you provide.
精彩评论