JDev
JDev

Reputation: 5558

How to switch views on orientation change

I'm currently in the midst of creating an application with six views, each view being dedicated to a combination of device type and orientation. In case you don't understand what I mean, I'll label all six combinations:

I know it seems a bit silly to go through all of this trouble, but it is kind of required in this situation. Anyway, I've been trying to attempt a combination between Apple's solution from the Programming Guide and a method given from an online tutorial found at TheAppCodeBlog.

ViewController.m --> ViewDidLoad method

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

ViewController.m --> orientationChanged method

- (void) orientationChanged:(NSNotification *)object
{
    UIDeviceOrientation deviceOrientation = [[object object] orientation];
    if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 480.0)
    {
        self.view = self.portrait4View;
    }
    else if ((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 480.0)
    {
        self.view = self.landscape4View;
    }
    else if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 568.0)
    {
        self.view = self.portrait5View;
    }
    else if ((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 568.0)
    {
        self.view = self.landscape5View;
    }
    else if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 1024.0)
    {
        self.view = self.portraitPadView;
    }
    else
    {
        self.view = self.landscapePadView;
    }

}

Interface Builder setup for each view:

Interface Builder

I'm also receiving a some errors for Expected identifier:

Mini Window

Update

Whenever I launch the app, I just get a black screen. Here is what my AppDelegate.m looks like:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_Portrait5" bundle:nil];
    }
    
    else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 480.0) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_Portrait4" bundle:nil];
    }

    else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_PortraitPad" bundle:nil];
    }
}

Upvotes: 0

Views: 177

Answers (1)

Andy Obusek
Andy Obusek

Reputation: 12842

Your error related to Expected Identifier is because your parens are wrong. Your if condition should be (and I see the same error in a couple of the other conditions as well):

else if (((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 480.0)

Upvotes: 1

Related Questions