繁体   English   中英

如何使用一个ViewController文件加载景观xib?

[英]How can I load a landscape xib using one ViewController file?

我有一个通用应用程序。 我使用单独的xibs进行纵向和横向视图。 当我处于风景中时,我可以使用该应用程序检测方向并将BOOL的值更改为true。 我想知道当BOOL为true时如何加载我的风景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

编辑

看来您需要在这里工作

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

或者如果您希望在实例化时命名笔尖

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

MyViewController *viewController = [[MyViewController alloc] initWithNibName:nibName bundle:nil];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM