3

抱歉,伙计们,我知道已经有很多类似的问题可用。我尝试了所有解决方案,但没有为我工作。

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

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

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

谢谢

马尤尔

4

5 回答 5

4

我建议你改为这样做:

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

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

于 2013-04-13T17:55:24.777 回答
1

我从您的描述中看到的明显问题是您在 nib 中将自定义类设置为“RootViewController”,但实际上您在代码中实例化了“UIViewController”。

你应该做的是:

viewController3 = [[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease];

否则,当运行时加载您的 nib,并尝试在您的 nib 中设置那些 RootViewController 特定的插座时,运行时将无法在普通 UIViewController 中找到这些插座,因此会崩溃。

于 2013-04-13T17:26:14.700 回答
0

我认为您不必为 iPhone5 和 iPhone4、4S 等使用两个不同的 .xib。如果您想更改图像、标签、按钮等的大小,这就是使用 Spring 和 Structs 的原因。您还可以使用您在问题中编写的代码(在您的 .m 文件中......)以编程方式设置大小

我以前也犯过这个错误。当我以前运行程序时,我总是想出错误

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
 reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "View" nib but
 the view outlet was not set.' *** First throw call stack:(0x1c93012 0x10d0e7e 0x1c92deb 
0xf58c8 0xf5dc8 0xf5ff8 0xf6232 0x453d5 0x4576f 0x45905 0x4e917 0x2b7f 0x12157 0x12747 
0x1394b 0x24cb5 0x25beb 0x17698 0x1beedf9 0x1beead0 0x1c08bf5 0x1c08962 0x1c39bb6 
0x1c38f44 0x1c38e1b 0x1317a 0x14ffc 0x25fd 0x2525 0x1)libc++abi.dylib: 
terminate called throwing an exception

这是因为“未设置笔尖出口”

所以,我认为你应该使用 Springs 和 structs 或者以编程方式..

于 2013-04-13T09:54:00.003 回答
0

哇,我真是个傻瓜,我没有意识到我没有正确加载笔尖。唯一我错的是这行代码

我写了 viewController3 = [[[UIViewController alloc] initWithNibName:@"RootViewController5"bundle:nil] autorelease]; 代替

 [[NSBundle mainBundle] loadNibNamed:@"RootViewController5" owner:self options:nil];

所以我的最终代码看起来像这样并且工作得很好

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if(result.height == 480)
        {
            // iPhone Classic
            [[NSBundle mainBundle] loadNibNamed:@"RootViewController" owner:self options:nil];
        }
        if(result.height == 568)
        {
            // iPhone 5
            [[NSBundle mainBundle] loadNibNamed:@"RootViewController5" owner:self options:nil];
        }
    }

感谢这个链接链接到正确的答案

于 2013-04-15T11:28:12.807 回答
0

I would recommend you to :

use story board, instead of XIB files when you instantiate a VC, ex:

    myVC* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"myVCStoryBoardID"];

[self.navigationController pushViewController:vc animated:YES];  

This helps keep VC design under the same roof, and to use powerful features of storyboarding.

Then to check that Auto-layout is active on your story board controllers, and insure that the constrains on one, say, label, map to the boundary of other elements (very important), above and below. This materialises by dotted blue lines when you move it. In most case, the run-time will be able to align everything, regardless of screen height.

I understand that there might be some nasty edge cases in this regards, so you might have to adjust stuff manually. Unless you have complex graphics, it's always possible to work with Y coordinate in the [0,1] interval, and once you have to set a frame, use [[UIScreen mainScreen] bounds].size to get the appropriate value, rounded to the nearest integer.

If all the above fails, then you might have to create separate VC, but in my view, that's not really what the SDK is intended for.

good luck!

于 2013-04-13T13:37:49.590 回答