Question

Here is my question :

I have an app running smoothly on iOS 3.0 I've been working a long time to port it to 4.0 and to include the new features.

I added iAds using Interface Builder.

I know that I have to define it programmatically to still support 3.0 devices. I weak linked the framework. But the app crashes when loading the NIB.

Is there a way to load NIB's depending on the firmware version ? Let's say I have FirstView.xib and FirstView3X.xib

How can I choose which one will be loaded depending on the firmware ?

Thanks

PS: It's my first question on StackOverflow !

Was it helpful?

Solution

Congratulations to your first question :-) How do you load the nibs? It’s a good idea to split the interface into several files. I usually use a separate nib per every controller and load them using the initWithNibName:bundle: initializer. Since you supply the nib name here, you could easily customize the loading behaviour.

If you load the nibs using the tab bar controller, you probably have all the tabs in one nib. I think I would break the tabs into separate nibs and load them programmatically:

id tab1 = [[UIViewController alloc] initWithNibName:@"Tab1" bundle:nil];
id tab2 = [[UIViewController alloc] initWithNibName:@"Tab2" bundle:nil];
id tab3 = [[UIViewController alloc] initWithNibName:@"Tab3" bundle:nil];
[tabBarController setViewControllers:[NSArray arrayWithObjects:
    tab1, tab2, tab3, nil]];

And since you would now have the control over the nib names, you could easily detect the firmware version and load the correct nib:

id tab1 = [[UIViewController alloc] initWithNibName:
    [self nibNameForCurrentFirmware] bundle:nil];

As for the firmware version detection itself, I think you could use [UIDevice systemVersion].

OTHER TIPS

You can pull the current version from the UIDevice object:

NSString *osVersion = [[UIDevice currentDevice]systemVersion];
NSArray *ver = [osVersion componentsSeparatedByString:@"."];

If you're happy with comparing strings for single values (e.g. [[ver objectAtIndex:0] isEqualToString:@"3"]). You might want to convert the version numbers to integers so you can compare them for range (e.g., osMajor >= 4).

You can also check for certain classes to exist and load the appropriate NIB based on that:

if (NSClassFromString(@"MKMapView") != nil) {
    /* Do map stuff... */
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top