繁体   English   中英

更改不同设备的视图

[英]Change the view for different devices

我有一个仅适用于iPhone的项目(版本4、5和6)。在我的情况下,我使用的是xib文件,在这种情况下,我用xib文件创建了一个名为ViewController的类,然后又创建了三个xib文件,导致您具有以下结构:

ViewController.h
ViewController.m
ViewController.xib
ViewController4inch.xib
ViewController47inch.xib
ViewController55inch.xib

在我的ViewController.m中,我将以下代码放入ViewDidLoad中,以识别iphone的版本并将视图更改为User:

- (void)viewDidLoad {
    [super viewDidLoad];

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        ViewController *extratoVC = [[ViewController alloc] init];

        if([UIScreen mainScreen].bounds.size.height == 480){

            // 4-4s
        extratoVC = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

        }

        if([UIScreen mainScreen].bounds.size.height == 568){

            // 4-inch
        extratoVC = [[ViewController alloc] initWithNibName:@"ViewController4inch" bundle:nil];

        }

        if([UIScreen mainScreen].bounds.size.height == 667){

            // 4.7-inch
        extratoVC = [[ViewController alloc] initWithNibName:@"ViewController47inch" bundle:nil];

        }

        if([UIScreen mainScreen].bounds.size.height == 736){

            // 5.5-inch
        extratoVC = [[ViewController alloc] initWithNibName:@"ViewController55inch" bundle:nil];

        }

        [self presentViewController:extratoVC animated:YES completion:nil];// this line causing problem that you see below...

    }

}

不幸的是,此代码不起作用,并且出现以下错误消息:

Thread1: EXEC_BAD_ACCESS (code = 2, address = 0xbf72bfbc) 

在我的项目中,所有视图将由类ViewController.h / .m处理

就我而言,我正在处理.xib文件,并且不想使用情节提要板,任何人都知道为什么会发生此问题以及如何解决此问题?

这是不好的做法。 但是,如果要查找代码中断的位置,则可以添加异常断点。 像这里这样解释: https : //developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html

您正在初始化viewDidLoad的视图。 在初始化过程之后,总是调用此方法。 您不能在那里初始化视图。

您在哪里初始化ViewController 此时,您应该使用正确的xib文件名调用initWithNibName: :。 然后,呈现viewController。

除此之外,我强烈建议您使用更新的方法来实现此目的。 例如,您可以尝试使用Size类,以便仅为多个设备配置一个故事板。 此处: http : //www.learnswift.io/blog/2014/6/12/size-classes-with-xcode-6-and-swift

希望这可以帮助!

暂无
暂无

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

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