本文介绍了无法在Xcode 4.5中为Single ViewController类加载两个笔尖(.XIB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,各位,我知道已经有很多类似的问题.我尝试了所有解决方案,但对我没有任何帮助.

Sorry Guys in advance, I know there are already plenty similar questions are available. I tried all solutions but didn't work any of them for me.

我正在使用Xcode 4.5.2,并且为iphone5/ios6使用了两个xib 1> RootViewController5 ,对于所有其他设备, 2> RootViewController ,这两个笔尖文件都只有一个ViewController名为 RootViewController .在两个nib文件的文件所有者中,我都选择了自定义"类检查器中的 RootViewController 类.

I'm Using Xcode 4.5.2 and using two xibs for iphone5/ios6 1> RootViewController5 and for all other devices 2> RootViewController these both nib file has single ViewController named RootViewController.In both the nib file's File owner I have selected RootViewController class in Custom class inspector.

现在在ViewDidLoad方法中,我正在尝试加载两个这样的笔尖

Now in ViewDidLoad method I'm trying to load two nibs like this

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

但是没有运气.请告知我在哪里弄错了.

But no luck. Please advise where I'm getting it wrong.

谢谢

Mayur

推荐答案

我建议您改为执行以下操作:

I suggest you do something like this instead:

- (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语句时,如果两个都不正确,则将使应用程序崩溃,并且实际上不是很好的编码.

That is if your RootViewController is named just that. And by doing it this way you save yourself from future crashes if they should decide to add another size of an iPhone/iPod.As you are using two if statements, if neither is true it will crash the app and is really not good coding.

一种好的做法是,始终尝试着去思考并为未来做计划,如果他们应该发布另一个屏幕尺寸,那看起来就不好了,但至少不会崩溃.

A good practice is to always try and think ahead and plan for the future, if they should release another screen size it wouldn't look good but would at least not crash.

这篇关于无法在Xcode 4.5中为Single ViewController类加载两个笔尖(.XIB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 01:10