无法在 Xcode 4.5 中为单个 ViewController 类加载两个笔尖 (.XIB) [重复]

2024-01-28

提前抱歉,我知道已经有很多类似的问题了。我尝试了所有解决方案,但没有任何一个对我有用。

我正在使用 Xcode 4.5.2 并使用两个用于 iphone 5/ios 6 的 xib1> 根视图控制器5以及所有其他设备2> 根视图控制器这两个 nib 文件都有一个名为的 ViewController根视图控制器.在我选择的两个 nib 文件的文件所有者中根视图控制器自定义类检查器中的类。

现在在 ViewDidLoad 方法中我尝试加载两个笔尖,如下所示

 if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        UIViewController *viewController3;
        if(result.height == 480)
        {
          viewController3 = [[[UIViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease];
        }
        if(result.height == 568)
        {
        viewController3 = [[[UIViewController alloc] initWithNibName:@"RootViewController5" bundle:nil] autorelease];

            NSLog(@"iphone 5 123");
        }
    }

我也尝试过下面的代码

 if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        RootViewController *viewController3;
        if(result.height == 480)
        {
          viewController3 = [[[UIViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease];
        }
        if(result.height == 568)
        {
        viewController3 = [[[UIViewController alloc] initWithNibName:@"RootViewController5" bundle:nil] autorelease];

            NSLog(@"iphone 5 123");
        }
    }

但没有运气。请指教我哪里写错了。

Thanks

Mayur


我建议你做这样的事情:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        if([UIScreen mainScreen].bounds.size.height == 568.0)
        {
            //Use iPhone5 VC
            self = [super initWithNibName:@"RootViewController-568h" bundle:nibBundleOrNil];
        }
        else{
            //Use Default VC
            self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        }
    }
    return self;
}

也就是说,如果您的 RootViewController 就是这样命名的。通过这样做,如果他们决定添加其他尺寸的 iPhone/iPod,您就可以避免将来发生崩溃。 当您使用两个 if 语句时,如果两者都不成立,则会导致应用程序崩溃,并且确实不是一个好的编码。

一个好的做法是始终尝试提前思考并为未来做好计划,如果他们发布其他屏幕尺寸,它看起来会不太好,但至少不会崩溃。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在 Xcode 4.5 中为单个 ViewController 类加载两个笔尖 (.XIB) [重复] 的相关文章

随机推荐