views:

335

answers:

2

Hello,

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!

+2  A: 

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];
}
+1  A: 

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];
    }
}
bstahlhood
The view is going to have different functionality though. Different buttons and actions... So I was thinking I would have two separate classes and xib files. If featureWasPurchased then create homePurchaseView with homePurchaseView xib file. If I go this route I'll need to decide which class to load before I the ViewDidLoad method inside the homePurchaseView class is called.
jmurphy
That is why I was saying you have a main view with a container view that handles the decision making process and loads the appropriate view. You can still have then be separate classes and XIBs and then just initialize them in the above logic.
bstahlhood
jmurphy
Yup. That way you can keep your current setup and still accomplish what you want without too much structural change. You're welcome!
bstahlhood
Your suggestion worked! Thanks again for your help!
jmurphy