jmurphy
jmurphy

Reputation: 1891

Decide in code which class and xib file to load on app launch

I'm changing my app to add an in app purchase. When I initially set up the app I used interface builder but now i need to decide which class & xib file to load on app launch based on whether or not a user has purchased a feature.

My app is set up with a tab bar controller that has a naviagion controller for each tab. Inside the MainWindow.xib file I've told IB to use the HomeView.xib and HomeView class. Now I need to check if a feature has been purchased and load either HomeView or HomeView2. I don't know where in the app to tell the naviagionController which class and view to load as I previously set all this up in interface builder.

Can I make this decision inside the HomeViewNavigationController instead of IB automatically making this decision for me?

Thanks in advance for your help!

Upvotes: 1

Views: 727

Answers (2)

bstahlhood
bstahlhood

Reputation: 2006

Keep it simple.

In HomeView.xib, have two different views you can load. Have the view of the UIViewController in the xib be the "container view". Do your detection code in the viewDidLoad of your HomeView controller and then add the proper view as a subview.

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (featureWasPaidFor)
    {
        [self.view addSubview:self.homePurchaseView];
    } else {
        [self.view addSubview:self.homeNonPurchaseView];
    }
}

Upvotes: 1

Saurabh Sharan
Saurabh Sharan

Reputation:

You set the xib you want to load in your Info.plist. At launch time the system will automatically load this xib. While its easy to change the xib name in the Info.plist file, it'll get rejected by Apple.

Here's what I would do: don't use xibs, at least for the two you mentioned. Instead, create everything in applicationDidFinishLaunching:(NSNotification *)notification

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
   UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

   UITabBarController *t = [[UITabBarController alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)];
   // add the other view controllers

   if(/* check for which xib to load */)
   {
      // change whatever you need to change
   }
   else
   {
      // ...
   }

   [window addSubview:[t view]];
   [window makeKeyAndVisible];
}

Upvotes: 1

Related Questions