ios - 在 iOS 中定位时加载另一个 .xib

标签 ios objective-c

我使用下面的代码来实现它,但它不起作用。请给我一些建议。非常感谢。

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)  name:UIDeviceOrientationDidChangeNotification  object:nil];

}

orientationChanged() 函数:

  - (void)orientationChanged:(NSNotification *)notification{
        [self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
    }

adjustViewsForOrientation() 函数:

- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {

    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        NSLog(@"load portrait view");
        [[NSBundle mainBundle] loadNibNamed:@"DashBoardViewController" owner:self options:nil];
    }
    else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
        NSLog(@"load landscapeview");
        [[NSBundle mainBundle] loadNibNamed:@"DashBoardLandscapeView" owner:self options:nil];
    }
}

运行时总是显示 DashBoardViewController.xib。在 lanscape 中,它仍然显示 DashBoardViewController.xib。我不知道为什么。

最佳答案

这是因为您实际上没有设置 View 。

你需要做类似的事情:

self.view = [[NSBundle mainBundle] loadNibNamed:@"DashBoardLandscapeView" owner:self options:nil] objectAtIndex:0];

这将从 Xib 文件加载 View ,并将当前 View 设置为该 View 。

不要忘记添加 objectAtIndex:0,如代码中所示。

关于ios - 在 iOS 中定位时加载另一个 .xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20721389/

相关文章:

iphone - 根据设备类型选择不同的 Storyboard

ios - 当应用程序暂停时,我如何知道 NSURLSessionUploadTask 是否正常工作?

iPhone SDK : POSTing NSData with NSMutableURLRequest results in mysterious crash

objective-c - 使用 Cocoa 的简单 OpenGL 示例

ios - if(!array.count)是否检查数组是否为空?

iphone - 将常规 xcode 应用程序项目转换为 iOSOpenDev

ios - 如何在拖动 UIView 时实现平滑的动画

iOS(结构): Crashlytics crashing app on launch

ios - 关于Core Data的两个问题

objective-c - Xcode 4.4 中架构 x86_64 的 undefined symbol