我可以将两个 xib 与一个视图控制器一起使用吗 - 重新:移植到 iPhone 5 [英] Can I use two xibs with one viewcontroller - re: porting to iPhone 5

查看:26
本文介绍了我可以将两个 xib 与一个视图控制器一起使用吗 - 重新:移植到 iPhone 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚向应用商店提交了我的第一个应用(是的刚刚被批准!).我现在想更新它以使用(看起来更好)更大的 iPhone 5 屏幕.除了为更大的屏幕稍微改变布局之外,我不打算改变任何东西.

I just submitted my first app to the app store (yay it was just approved!). I now want to update it to work with (look nicer on) the larger iPhone 5 screen. I don't intend to change anything other than to change the layout a bit for the larger screen.

注意:我不想让我当前的 xib 被拉伸.

NOTE: I don't want to have my current xib stretched.

是否可以创建两个 xib 文件(即:复制我当前主屏幕的 xib 文件)并将它们都挂接到视图控制器中,这样当应用程序启动时,应用程序检测是否有 iPhone5 屏幕或更早的屏幕.然后,根据设备的不同,向用户显示不同的屏幕.

Is it possible to create two xib files (ie: copy my current xib file for the main screen) and hook them both into the view controller and have it so that when the app launches, the app detects if there is an iPhone 5 screen or an earlier screen. Then, depending on which device it is, show the user a different screen.

我打算让底层应用保持不变.我想要的只是为 iPhone 5 用户呈现一个略有不同(更高)的屏幕,并为新布局移动一些按钮/项目.否则我不会在界面中添加或删除任何内容.

I intend for underlying app to remain the same. All I want is to present a slightly different (taller) screen for iPhone 5 users with a few buttons/items moved around for the new layout. I otherwise won't be adding or removing anything from the interface.

这个问题/答案展示了如何在 iPhone 或iPad 视图.所以这个一个.两者都有帮助,但我不知道如何针对用户使用更大屏幕的 iPhone 5 或 iPhone 4S 及更低版本的情况进行修改.此外,他们假设有两个视图控制器.我只想要一个视图控制器,因为视图控制器逻辑中绝对没有任何变化 - 只有屏幕上对象的位置发生变化,而这一切都在 XIB 中完成.

This SO question/answer shows how to switch between an iPhone or iPad view. So to does this one. Both are helpful but I don't know how to modify this for the circumstance where the user is using an iPhone 5 with a larger screen or an iPhone 4S and below. Also, they assume two view controllers. I only want ONE view controller since absolutely NOTHING in the view controller logic changes - only the placement of the objects on the screen change and that is all done in the XIB.

我应该认为答案应该是视图控制器 iteslf 评估它正在运行的设备,然后提供适当的 xib?是的?没有?

I should think the answer should be that the view controller iteslf assesses what device it is running on then presents the appropriate xib? Yes? No?

如果是这样,我会怎么做?

If so, how would I go about this?

推荐答案

[Revised with Complete Answer on : Oct 7, 2012]

经过大量研究,我找到了答案,部分在 此 SO 页面(显示如何检测您的应用正在运行的 iPhone 版本) 部分这个 SO 页面(展示如何让两个 xib 共享相同的文件所有者".拼图的最后一部分(通过 loadNibNamed: 方法加载单独的 xib)我在 The Big Nerd Ranch 的优秀 iOS 编程文本的第 10 章中找到. 方法如下:

After significant research I found the answer, partly on this SO page (which shows how to detect which iPhone version your app is running on) and partly this SO page (showing how to have two xib's share the same 'File's Owner'. The final piece of the puzzle (loading separate xib's via the loadNibNamed: method) I found in chapter 10 of The Big Nerd Ranch's excellent iOS Programming text. Here's how:

  1. 创建第二个xib(文件,新建...,文件,选择用户界面",选择空"并保存.这将创建新的xib.在下面的示例中,我的经典xib(对于3.5" iPhones) 被命名为 TipMainViewController.xib.我将新的 iPhone 5 xib 保存为名称TipMainViewController-5.xib"

  1. Create a second xib (File, New..., File, select 'User Interface', select 'Empty' and save it. This creates the new xib. In the example below, my classic xib (for 3.5" iPhones) was named TipMainViewController.xib. I saved the new iPhone 5 xib with the name 'TipMainViewController-5.xib'

使新的 xib 的文件所有者"成为与现有 xib 相同的 ViewController.为此,在新的 xib 文件中,选择文件所有者".然后在身份检查器"中选择现有的视图控制器作为自定义类.就我而言,我选择了TipMainViewController".

Make that new xib's 'File's Owner' the same ViewController as your existing xib. To do this, in the new xib file, select 'File's Owners'. Then in the 'Identity Inspector' select the existing View Controller as the Custom Class. In my case I selected 'TipMainViewController'.

将一个新的 UIView 拖到新的 xib 的空画布上.在新的 UIView 的属性检查器中,将大小"属性设置为Retina 4 Full Screen"

Drag a new UIView onto the new xib's empty canvas. In the new UIView's attribute inspector set the 'Size' attribute to 'Retina 4 Full Screen'

选择现有经典"3.5" xib 中的所有内容 - 例如:所有控件、按钮、选择器、标签等.将它们复制并粘贴到新的 iPhone 5 xib 中.调整大小/移动等.它们以针对 iPhone 的 4 英寸显示屏进行优化.

Select all the contents in the existing 'Classic' 3.5" xib - eg: all your controls, buttons, selectors, labels etc. Copy them and paste them into the new iPhone 5 xib. Resize/move etc. them to optimize for the iPhone's 4" display.

像创建原始 xib 时一样,建立与文件所有者的所有连接.

Make all the connections to/from File's Owner as you did when you created your original xib.

最后,在单个"ViewController 的viewDidLoad"方法中,插入以下逻辑(当然使用您的 nib/xib 名称):

Finally, in the 'viewDidLoad' method of your 'single' ViewController, insert the following logic (using your nib/xib names of course):

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

这篇关于我可以将两个 xib 与一个视图控制器一起使用吗 - 重新:移植到 iPhone 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆