开发者

Initialize custom class according to user selection

开发者 https://www.devze.com 2022-12-08 10:22 出处:网络
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 f

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号