1

I have a universal app. I'm using separate xibs for the portrait and landscape views. I have the app detecting the orientation and changing the value of a BOOL to true when I'm in landscape. I want to know how to load my landscape xib when that BOOL is true. I've tried several different methods to achieve this, but nothing has worked. Any input on this matter would be most appreciated. I can update this post to include any code snippets necessary. Thanks in advance.

edit: I want to do all of this in one ViewController class, and only for the iPad... not the iPhone. I have all that part worked out. I just need to load the landscape xib.

edit: In my viewDidLoad I'm doing this:

if (userDevice.orientation == UIDeviceOrientationLandscapeLeft || userDevice.orientation == UIDeviceOrientationLandscapeRight) {
    landscape = YES;
}

Here's my main view controller .m:

@implementation PassportAmericaViewController

@synthesize browseViewButton, webView, mainView, lblMemberName, menuOpen, internetActive, hostActive, isUsingiPad, portrait, landscape;


- (void)viewDidLoad {
menuOpen = NO;

UIDevice* userDevice = [UIDevice currentDevice];
if (userDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    isUsingiPad = YES;
}
if (isUsingiPad)
    if (userDevice.orientation == UIDeviceOrientationLandscapeLeft || userDevice.orientation == UIDeviceOrientationLandscapeRight) {
        landscape = YES;

    }
[self checkForKey];
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
}

-(void)viewWillAppear:(BOOL)animated{

[self.navigationController setNavigationBarHidden:YES];
}

-(void) checkForKey{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int regCheck = [defaults integerForKey:@"registration"];
if (isUsingiPad) {
    if (regCheck == 0) {

        RegistrationViewController *regView = [[RegistrationViewController alloc]
                                               initWithNibName:@"RegistrationView-iPad" bundle:[NSBundle mainBundle]];
        regView.isUsingiPad = YES;
        [self.navigationController pushViewController:regView animated:YES];

    }else if (regCheck == 1) {
        @try {
            NSString *mbrFirstName = [defaults objectForKey:@"firstName"];
            NSString *mbrLastName = [defaults objectForKey:@"lastName"];

            NSMutableString *name = [[NSMutableString alloc] initWithString:mbrFirstName];
            [name appendString:@" "];
            [name appendString:mbrLastName];

            lblMemberName.text = name;
        }
        @catch (NSException *exception) {

        }        
    }
}else{
    if (regCheck == 0) {

        RegistrationViewController *regView = [[RegistrationViewController alloc]
                                               initWithNibName:@"RegistrationView" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:regView animated:YES];

    }else if (regCheck == 1) {
        @try {
            NSString *mbrFirstName = [defaults objectForKey:@"firstName"];
            NSString *mbrLastName = [defaults objectForKey:@"lastName"];

            NSMutableString *name = [[NSMutableString alloc] initWithString:mbrFirstName];
            [name appendString:@" "];
            [name appendString:mbrLastName];

            lblMemberName.text = name;
        }
        @catch (NSException *exception) {

        }        
    }
}
}

-(IBAction) openBrowseView{

if (isUsingiPad && landscape) {
        BrowseViewController *browseView = [[BrowseViewController alloc]
                                            initWithNibName:@"BrowseView-iPadLandscape" bundle:[NSBundle mainBundle]];
        browseView.isUsingiPad = YES;
        [self.navigationController pushViewController:browseView animated:YES];
    }else if (isUsingiPad){
    BrowseViewController *browseView = [[BrowseViewController alloc]
                                        initWithNibName:@"BrowseView-iPad" bundle:[NSBundle mainBundle]];
    browseView.isUsingiPad = YES;
    [self.navigationController pushViewController:browseView animated:YES];
    }else{
    BrowseViewController *browseView = [[BrowseViewController alloc]
                                        initWithNibName:@"BrowseView" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:browseView animated:YES];

}
}     

-(IBAction) openViewMore{

if (isUsingiPad) {
    ViewMoreViewController *viewMoreView = [[ViewMoreViewController alloc]
                                            initWithNibName:@"ViewMoreView-iPad" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:viewMoreView animated:YES];
    viewMoreView.isUsingiPad = YES;
}else{
    ViewMoreViewController *viewMoreView = [[ViewMoreViewController alloc]
                                            initWithNibName:@"ViewMoreView" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:viewMoreView animated:YES];
}       
}

-(IBAction) callTollFree:(id)sender {

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:8002837183"]];

}

-(IBAction)clickToJoin:(id)sender {

if (isUsingiPad) {
    webView = [[WebViewController alloc]
               initWithNibName:@"WebView-iPad" bundle:[NSBundle mainBundle]];
    webView.url=@"http://www.passport-america.com/Members/JoinRenew.aspx";
    [self.navigationController pushViewController:webView animated:YES];
    webView.isUsingiPad = YES;
}else {
    webView = [[WebViewController alloc]
               initWithNibName:@"WebView" bundle:[NSBundle mainBundle]];
    webView.url=@"http://www.passport-america.com/Members/JoinRenew.aspx";
    [self.navigationController pushViewController:webView animated:YES];
}
}

-(IBAction) iPadContactUs:(id)sender {

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"mailto:info@passport-america.com"]];

}

-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if ([animationID isEqualToString:@"slideMenu"]){
    UIView *sq = (__bridge UIView *) context;
    [sq removeFromSuperview];

}
}

-(void) positionViews {

if (isUsingiPad) {
    UIInterfaceOrientation destOrientation = self.interfaceOrientation;
    if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        PassportAmericaViewController *homeView2 = [[PassportAmericaViewController alloc]
                                             initWithNibName:@"PassportAmericaViewController-iPad" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:homeView2 animated:YES];
        homeView2.isUsingiPad = YES;
        homeView2.portrait = YES;
        homeView2.landscape = NO;
    }else{
        PassportAmericaViewController *homeView2 = [[PassportAmericaViewController alloc]
                                             initWithNibName:@"PassportAmericaViewController-iPadLandscape" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:homeView2 animated:YES];
        homeView2.isUsingiPad = YES;
        homeView2.portrait = NO;
        homeView2.landscape = YES;

    }
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (isUsingiPad) {
    return YES;
}else{
    // Return YES for supported orientations
    return NO;
}
}

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration {
if (isUsingiPad) {
    [self positionViews];
}else{

}
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}


@end
tallybear
  • 417
  • 3
  • 17

1 Answers1

1

Edit

It looks like you need to do your work here

BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
NSString *nibName = isLandscape ? @"landscapeNibName" : @"portraitNibName";

RegistrationViewController *regView = [[RegistrationViewController alloc] initWithNibName:nibName bundle:[NSBundle mainBundle]];

I'm not sure where you are getting stuck...

- (id)init;
{
    BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
    NSString *nibName = isLandscape ? @"landscapeNibName" : @"portraitNibName";
    
    self = [super initWithNibName:nibName bundle:nil];
    if (self) {
        // any other init stuff
    }
    return self;
}

or if you prefer to name the nib when you instantiate

BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
NSString *nibName = isLandscape ? @"landscapeNibName" : @"portraitNibName";

MyViewController *viewController = [[MyViewController alloc] initWithNibName:nibName bundle:nil];
Community
  • 1
  • 1
Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • I'm getting stuck on where to load the new xib. I have the BOOL called landscape already being set to yes. I've tried to load the xib in viewWillAppear and I've even tried to set up an if statement in viewDidLoad to try to get it to override the original xib. I get stuck in an infinite loop. – tallybear May 03 '12 at 16:46
  • Are you wanting to load the viewController in a certain orientation? or are you trying to cope with orientation changes? – Paul.s May 03 '12 at 16:50
  • When the orientation is landscape, I want it to load the landscape xib instead of the portrait xib. The orientation changes I have under control. When the app loads, I can change to the landscape xib from the portrait xib without issue. I just want to bypass the portrait xib altogether when I'm in landscape. If I get that worked out... I can pass along that landscape BOOL and be gravy from that point on. – tallybear May 03 '12 at 16:56
  • So have you tried the above code? Both will load the correct nib. Either use the first by placing it in the viewcontrollers init method or use the second method at the point where you actually alloc/init the view controller – Paul.s May 03 '12 at 17:12
  • I tried it, but I don't have an init method. I tried doing some stuff in the viewDidLoad, but that causes me to slip into an infinite loop. I tried creating an init method but it's never called unless I call it in the viewDidLoad... which also causes a loop. I've never tried to do this before... that's why I'm having so much trouble. haha. – tallybear May 03 '12 at 18:13
  • I just updated my question to show my .m. To transition I'm using [self.navigationController pushViewController:nibName animated:YES]; but now that's causing me a whole other issue I have to work out. Haha. – tallybear May 03 '12 at 19:54
  • I'm starting to think this isn't going to work the way I want it to. – tallybear May 03 '12 at 20:44
  • I'm using a custom back button in my app. The app is already on the app store if you want to look at it. Just look up Passport America. It's the one with the circle logo. Anyway... the back button uses [self.navigationController popViewControllerAnimated:YES]; to go back to the previous view. When I switch to landscape view, then back to portrait, and hit the back button... the landscape view is loaded in portrait view. I may have to do a complete overhaul of how I'm implementing my views. – tallybear May 03 '12 at 21:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10860/discussion-between-tallybear-and-paul-s) – tallybear May 03 '12 at 21:01