views:

106

answers:

1

I have a preference that, when set, forces my application to perform some synchronization on startup.

Can I use IB to display a different initial view based on this setting?

Is there a standard way to enable this behavior?

A: 

Assuming you have a property on your app delegate that is set during your synchronization, in the initial view controller's initWithNibNamed: method check the value synced by the app delegate and load the appropriate nib by calling [super initWithNibNamed:@"thisNibInsteadOfThatNib"];

EDIT: Show code to launch a different view depending on some condition at launch

// AppDelegate.h
#import <UIKit/UIKit.h>

@interface AppDelegate : NSObject <UIApplicationDelegate>
{
    UIWindow *window;
    UIViewController *firstViewController;
}
@property {nonatomic, retain} UIWindow *window;
@end

// AppDelegate.m
#import AppDelegate.h
#import ViewControllerOne.h
#import ViewControllerTwo.h

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    BOOL shouldLoadViewOne = \\ some value from preferences

    if (shouldLoadViewOne) {
        firstViewController = [[ViewOneController alloc] initWithNibName:@"ViewOneController" bundle:nil];
    } else {
        firstViewController = [[ViewTwoController alloc] initWithNibName:@"ViewTwoController" bundle:nil];
    }

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstViewController];

    [window addSubView:[navController view]];

    [window makeKeyAndVisible];

    return YES;
}

EDIT 2:

Make use of NSClassFromSting() and save the name of the firstViewController to load in the preferences.

// AppDelegate.h
#import <UIKit/UIKit.h>

@interface AppDelegate : NSObject <UIApplicationDelegate>
{
    UIWindow *window;
    id firstViewController;
}
@property {nonatomic, retain} UIWindow *window;

- (NSString *)firstViewControllerName;

@end

// AppDelegate.m
#import AppDelegate.h
#import ViewControllerOne.h
#import ViewControllerTwo.h

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSString *viewControllerName = [self firstViewControllerName];

    firstViewController = [[NSClassFromString(viewControllerName) alloc] initWithNibName:viewControllerName  bundle:nil];

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstViewController];

    [window addSubView:[navController view]];

    [window makeKeyAndVisible];

    return YES;
}

- (NSString *)firstViewControllerName
{
    NSString *defaultViewController = @"ViewOneController";
    NSString *savedFirstViewController = // string retrieved from preferences or other persistent store

    if (!savedFirstViewController)
        return defaultViewController;

    return savedFirstViewController;
}
falconcreek
alternatively, if you have different view controllers for each nib ( ViewOneController, ViewTwoController) initialize the the controller or the view that you need to load based on the condition from the sync in `applicationDidFinishLaunching:`
falconcreek
of course this assumes that this isn't an "internationalization" question which is handled automatically at runtime if you create the necessary nib files as per Apple documentation
falconcreek
Can you please elaborate on initializing 'didFinishLaunchingWithOptions:'?
Brandon
updated answer to show loading a different rootView depending on some condition.
falconcreek
Thank you. This is exactly what I was looking for.
Brandon
Added another alternative using NSClassFromString()
falconcreek
That's excellent, man. I really appreciate the great code! Your second example is really helpful too.
Brandon