通用应用程序 - 如何? [英] Universal app - how to?

查看:21
本文介绍了通用应用程序 - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xcode 4.2 并且正在编写通用应用程序.在开始一个新项目时,我选择了 SingleView 应用程序模板.XCode 添加了 ViewController1.h、ViewController1.m、ViewController1_iphone.xib 和 ViewController1_iPad.xib.我需要添加更多 UI 并单击 File...New...New File 并选择 UIViewController 子类模板并看到两个复选框(针对 iPad,用户界面使用 Xib).

I'm using Xcode 4.2 and in the process of writing a universal app. I selected SingleView Application template when starting with a new project. XCode added ViewController1.h, ViewController1.m, ViewController1_iphone.xib and ViewController1_iPad.xib. I need to add more UIs and clicked on the File...New...New File and selected UIViewController subClass template and seeing two checkboxes (Targeted for iPad, With Xib for User Interface).

我应该怎么做才能同时支持 iPad 和 iPhone,同时拥有共享相同代码的通用 .h 和 .m 文件.我是否需要在我的视图控制器中添加代码来检查它是 iPad 还是 iPhone?

What should I do here to support both iPad and iPhone while at the same time have a common .h and .m files that share the same code. Do I need to add code to check whether it is a iPad or iPhone by doing this in my view controllers?

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

此外,我还看到有人在谈论 ~iPad 和 ~iPhone.这是怎么回事?

Also, I have seen people talking about ~iPad and ~iPhone. What is this all about?

如果我理解正确,由于屏幕尺寸不同,我是否必须为 iPad 和 iPhone 分别设计 UI?

If I understand correctly, do I have to design the UI separately both for iPad and iPhone due to different screen sizes?

我在这里完全糊涂了.

请帮忙.

推荐答案

您可以添加两个笔尖(一个用于 ipad,一个用于 iphone),或者您可以添加一个可以为任一界面正确缩放的笔尖.通常,如果您制作的视图将覆盖整个屏幕或大部分屏幕,您会添加两个笔尖,如果您制作的小东西可能会在 iphone 上全屏显示,但您会添加一个笔尖ipad 上的弹出框.

You can either add two nibs (one for ipad and one for iphone), or you can add one nib that will properly scale for either interface. Normally you'd add two nibs if you're making a view that will cover all or most of the screen, and you'd add one nib if you're making something small that will, perhaps, be fullscreen on iphone but displayed in a popover on ipad.

波浪号后缀 ~ipad~iphone 在标题 资源编程指南中的iOS 支持特定于设备的资源".请注意,后缀完全是小写的,而不是您在问题中所写的驼峰式.这很重要,因为 iOS 使用区分大小写的文件系统.

The tilde suffixes ~ipad and ~iphone are described under the heading "iOS Supports Device-Specific Resources" in the Resource Programming Guide. Notice that the suffixes are entirely lower-case, not camel-case as you wrote in your question. This matters because iOS uses a case-sensitive filesystem.

当您使用 -[NSBundle pathForResource:ofType:]-[NSBundle URLForResource:withExtension:] 之类的 NSBundle 消息获取资源路径时,iOS 会首先查找后缀为 ~ipad~iphone 的资源文件,具体取决于当前设备.例如,假设您这样做:

When you get a path for a resource using an NSBundle message like -[NSBundle pathForResource:ofType:] or -[NSBundle URLForResource:withExtension:], iOS will first look for the resource file with a suffix of ~ipad or ~iphone, depending on the current device. For example, suppose you do this:

NSString *path = [[NSBundle mainBundle] pathForResource:@"setup" ofType:@"plist"];

  • 如果您在 iPhone 类型的设备(包括 iPod touch)上或在 iPhone 模式下的模拟器上运行它,iOS 将首先在您的应用程序包中查找名为 setup~iphone 的文件.plist.如果找到这样的文件,它将返回该文件的路径.如果它没有找到那个文件,它会返回 setup.plist 的路径.

    • If you run this on an iPhone-type device (including an iPod touch), or on the simulator in iPhone mode, iOS will first look in your app bundle for a file named setup~iphone.plist. If it finds such a file, it will return the path of that file. If it doesn't find that file, it will instead return the path to setup.plist.

      如果您在 iPad 类型的设备上或在 iPad 模式下的模拟器上执行此操作,iOS 将首先在您的应用程序包中查找名为 setup~ipad.plist 的文件.如果找到这样的文件,它将返回该文件的路径.如果它没有找到那个文件,它会返回 setup.plist 的路径.

      If you this on an iPad-type device, or on the simulator in iPad mode, iOS will first look in your app bundle for a file named setup~ipad.plist. If it finds such a file, it will return the path of that file. If it doesn't find that file, it will instead return the path to setup.plist.

      从捆绑包中获取资源的所有其他 API 都构建在 NSBundle 之上,因此它们都受益于这种特定于设备的查找.这意味着如果你使用 +[UIImage imageNamed:],它会自动使用特定于设备的图像,如果你的包中有一个.如果您使用 -[NSBundle loadNibNamed:owner:options:],它会自动加载特定于设备的笔尖 (.xib) 文件,如果您在捆绑.

      All of the other APIs that get resources from bundles are built on top of NSBundle, so they all benefit from this device-specific lookup. That means if you use +[UIImage imageNamed:], it will automatically use a device-specific image, if you have one in your bundle. And if you use -[NSBundle loadNibNamed:owner:options:], it will automatically load a device-specific nib (.xib) file, if you have one in your bundle.

      如果您使用后缀,这将简化您的代码.如果您创建 MyViewController~ipad.xibMyViewController~iphone.xib,您的应用程序将自动为当前设备加载正确的应用程序.您不必检查用户界面习惯用法;NSBundle 会为您检查.(您也可以使用名称 MyViewController~ipad.xibMyViewController.xib 并获得相同的效果.)

      This simplifies your code, if you use the suffixes. If you create MyViewController~ipad.xib and MyViewController~iphone.xib, your app will automatically load the correct one for the current device. You don't have to check the user interface idiom; NSBundle checks it for you. (You could also use the names MyViewController~ipad.xib and MyViewController.xib and get the same effect.)

      现在,您可能已经注意到,当您创建通用"项目时,Xcode 为您的项目文件提供了名为 ViewController1_iPhone.xibViewController1_iPad.xib 的文件,它们not 使用波浪号后缀,它包含代码以查看用户界面习惯用法并相应地选择文件名.为什么通用项目模板会这样做?我不知道,但它愚蠢.我建议您修复文件名以使用波浪号后缀并删除检查用户界面习惯用法的代码.

      Now, you may have noticed that when you created your "universal" project, Xcode gave your project files named ViewController1_iPhone.xib and ViewController1_iPad.xib, which do not use the tilde suffixes, and it included code to look at the user interface idiom and choose a filename accordingly. Why does the universal project template do this? I don't know, but it is stupid. I suggest you fix the filenames to use the tilde suffixes and rip out the code that checks the user interface idiom.

      这篇关于通用应用程序 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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