如何使用一个ViewController文件加载横向xib?

时间:2012-05-03 16:02:47

标签: objective-c ios ipad xib ios-universal-app

我有一个通用的应用程序。我正在使用单独的xib来绘制纵向和横向视图。当我在风景中时,我有应用程序检测方向并将BOOL的值更改为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

1 个答案:

答案 0 :(得分: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];
相关问题