Plies Neyo
Plies Neyo

Reputation: 247

Starting an App up with a different XIB

I want to start my app up with a different Xib. How would I do this?

Thanks

Upvotes: 1

Views: 1564

Answers (2)

bryanmac
bryanmac

Reputation: 39306

If you're using interface builder:

Under SupportingFiles, in -info.plist, look for a key named "Main nib file base name". Change that to the XIB you want it to load first

You can also take that entry out of the plist altogether an in main.m give it your appDelegate's name:

    int retVal = UIApplicationMain(argc, argv, nil, @"HelloViewAppDelegate");

Then in your appDelegate, you can manually load your first view controller based on your code and logic. Personally, I like this better because it's much clearer to - here's my delegate and code to load it. It doesn't have all the bindings in IB I need to remember.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    // Override point for customization after application launch.
    CGRect screenBounds = [[UIScreen mainScreen] applicationFrame];
    CGRect windowBounds = screenBounds;
    windowBounds.origin.y = 0.0;

    // init window
    [self setWindow: [[UIWindow alloc] initWithFrame:screenBounds]];

    // init view controller
    _mainViewController = [[MainViewController alloc] init];

    [[self window] addSubview:[_mainViewController view]];

    [self.window makeKeyAndVisible];
    return YES;
}

EDIT:

Answering your comment below. You pasted this invalid code:

// init view controller 
ViewController = [[ViewController alloc] init]; 
[[self window] addSubview:[ViewController view]]; 

That's not valid. you need an instance variable name. By referring to it as "ViewController" your attempting to call class member variables. If your class is called ViewController then it should be:

// notice the instance myviewController is of type ViewController
ViewController *myViewController = [[ViewController alloc] init]; 
// notice calling view against instance (myViewController)
[[self window] addSubview:[myViewController view]]; 

At this point, if it's not compiling you need to edit your question and paste your main.m and appDelegate exactly as is into the question.

Upvotes: 3

Tony
Tony

Reputation: 3478

The MainWindow.xib file is just a shell that provides the application's UIWindow. While you can put a UIViewController in MainWindow.xib, you can also just have an unconnected UIViewController outlet on your app delegate and pick which nib to load at runtime in your application:didFinishLaunchingWithOptions: method, e.g.:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {
  if (case1)
    self.viewController = [[[MyViewController1 alloc] initWithNibName:nil bundle:nil] autorelease];
  else if (case 2)
    self.viewController = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
  else
    self.viewController = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];

  [window addSubview:self.viewController.view];
  [window makeKeyAndVisible];

  return YES;
}

Upvotes: 0

Related Questions