Question

I am new to the UI part of iOS development. I am trying to create a universal iOS app. In Xcode 4.3, if a project is created using the Single View Application template and Universal device family, these files are created: AppDelegate, ViewController, ViewController_iPhone.xib, and ViewController_iPad.xib. These get me confused because in the previous versions of the Xcode, two subclasses of AppDelegate is also created; One for iPhone and the other for iPad. Anyway, for example, I have TestAppDelegate, TestViewController, TestViewController_iPhone.xib, and TestViewController_iPad.xib. If I ran the app on the iPad simulator, TestViewController_iPad.xib is displayed.

Then I added AlternateViewController. Afterwards I created AlternateViewController_iPad.xib. I selected this xib file and set the File's Owner Custom Class to AlternateViewController and set the Outlet view to the View under Objects. I wanted to see if I can display AlternateViewController_iPad so in the TestAppDelegate I did this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    if(NO){
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.viewController = [[[TestViewController alloc] initWithNibName:@"TestViewController_iPhone" bundle:nil] autorelease];
        } else {
            self.viewController = [[[TestViewController alloc] initWithNibName:@"TestViewController_iPad" bundle:nil] autorelease];
        }
        self.window.rootViewController = self.viewController;
    }
    else{
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.alternateScreenController = [[[AlternateViewController alloc] initWithNibName:@"AlternateViewController_iPhone" bundle:nil] autorelease];
        } else {
            self.alternateScreenController = [[[AlternateViewController alloc] initWithNibName:@"AlternateViewController_iPad" bundle:nil] autorelease];
        }
        self.window.rootViewController = self.alternateScreenController;
    }

    [self.window makeKeyAndVisible];
    return YES;
}

I run the code but, unfortunately, AlternateViewController_iPad.xib is not displayed.



I put a break point on the 'return YES;' part and, in the Debug Area, I can see that the variable self.window.rootViewController.nibBundle is set to nil. However, if TestViewController_iPad is used, the variable self.window.rootViewController.nibBundle is not nil.



I deleted the AlternateViewController and AlternateViewController_iPad.xib. Then I re-created them but now, on the 'Choose options for your new file:' popup, I reset the Class to AlternateViewController, the "Subclass of" to UIViewController, and checked "targeted for iPad" and "With XIB for user interface." Modified the TestAppDelegate for these new files and then re-run the application. Curiously, the AlternateViewController (iPad) is now displayed and the variable self.window.rootViewController.nibBundle is not nil.



Now, what am I missing here? Where is the bug? Is it with my code or with Xcode 4.3???



Additional question:
If TestViewController_iPad is displayed first and there is a button there that when clicked will display the AlternateViewController_iPad, how am I, exactly, going to do that?


UPDATE I have figured out the problem. What happens is that when a ViewController is created without the xib file, the method loadView is added in the new ViewController. To my understanding, this method is used when views are created programatically/manually. When using xib files or a storyboard, this must be removed. I removed it and everything works! :)

Was it helpful?

Solution

I have figured out the problem. What happens is that when a ViewController is created without the xib file, the method loadView is added in the new ViewController. To my understanding, this method is used when views are created programatically/manually. When using xib files or a storyboard, this must be removed. I removed it and everything works! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top