1

我有一个通用应用程序。我为纵向和横向视图使用单独的 xib。BOOL当我处于横向时,我有应用程序检测方向并将 a 的值更改为 true。我想知道当这BOOL是真的时如何加载我的景观 xib。我尝试了几种不同的方法来实现这一点,但没有任何效果。对此问题的任何意见将不胜感激。我可以更新这篇文章以包含任何必要的代码片段。提前致谢。

编辑:我想在一个 ViewController 类中完成所有这些,并且只针对 iPad……而不是 iPhone。我已经解决了所有问题。我只需要加载景观xib。

编辑:在我viewDidLoad做这个:

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

这是我的主视图控制器.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
4

1 回答 1

1

编辑

看起来你需要在这里做你的工作

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

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

我不确定你在哪里卡住了......

- (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;
}

或者如果您更喜欢在实例化时命名 nib

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

MyViewController *viewController = [[MyViewController alloc] initWithNibName:nibName bundle:nil];
于 2012-05-03T16:10:05.380 回答