通用 iPad/iPhone XIB - Xcode 4
我想制作一个具有两个不同 XIB 文件的通用应用程序。一款适用于 iPhone,一款适用于 iPad。他们使用相同的代码,只是不同的用户界面。我将如何创建一个“通用”应用程序?
谢谢。
I want to make a universal app which has two different XIB files. One for iPhone, and one for iPad. They use the same code, just different UIs. How would I create a "universal" app?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需复制 xib 文件,然后将其重命名为 FileName~ipad.xib,将其添加到您的项目中,iOS 将自动加载引用您设备的正确 xib 文件。
Just duplicate the xib file, then rename it to FileName~ipad.xib, add it to your project, iOS will automatically load the correct xib file refer to your device.
首先(您说您正在创建一个基于视图的应用程序),根据 iPhone 或 iPad 视图创建它。
这将为您提供一个应用程序委托、一个视图控制器和一个视图(根据您选择的选项针对 iPad 或 iPhone 进行定制)
现在添加另一个 xib,转到“文件”>“新建文件...查看对话框左侧的 iOS 组中的选择器“用户界面”。在右侧窗格中,选择“查看”并单击“下一步”,现在在创建 xib 时选择 iPad 或 iPhone(根据您最初选择的内容),选择它,然后在主窗格左侧选择文件所有者。然后,转到“实用程序”(右窗格)并选择“身份检查器”(顶部的第三个选项图标),将类更改为与创建基于视图的应用程序时创建的视图控制器相同。您可以在两个视图上以相同的方式绑定插座,但它们将共享相同的 viewController。
确定您的应用程序在运行时在哪个设备上运行,您可以使用约定
并根据此类条件语句加载视图。为了清楚起见,请记住您使用其名称加载笔尖,以便您可以选择与环境相关的笔尖(上面),框架将完成其余的工作。
请注意,它永远不会像您想象的那么简单(如果您以前从未这样做过),充分利用 iPad 空间的应用程序通常会在专用视图中工作得更好,尽管情况并非总是如此。任何动态添加的屏幕组件都需要进行这样的编码,同时考虑到屏幕空间的差异。
这很容易变成一篇文章,我建议你阅读一些内容,查看源代码并深入研究。通过实验你会学到很多东西。
As a start (you say you are creating a view based application), create it based upon either iPhone or an iPad view.
This will give you an appdelegate, a viewcontroller and a view (tailored for iPad or iPhone dependent upon which option you chose)
Now add another xib, go to File > New File... look on the left of the dialog and chooser "User Interface" in the iOS group. In the pane on the right, select View and click next, now choose iPad or iPhone (based on what you chose initially) when the xib is created, select it, then select the files owner on the left of the main pane. Then, go to Utilities (right pane) and choose Identity inspector (3rd option icon along the top) there change the class to be the same viewController as was created when you created your view based application. You can bind outlets just the same way on both views but they will share the same viewController.
Determining which device your app is running on at runtime, you can use the convention
and base the loading of views upon this kind of conditional statement. Just for clarity, remember you load a nib using its name so you can choose the nib relevant to the environment (above) and the framework will do the rest.
Be aware that it is never as straightforwards as you might think (if you've never done it before) Apps that make best use of the iPad's real estate generally tend to work better with dedicated views, although this is certainly not always the case. Any dynamically added screen components will need to be coded as such, taking into account the difference in screen space.
This could easily turn into an essay, I suggest you do some reading, check out source code and dive in. You'll learn a lot just by experimenting.
像这样创建一个宏:
命名您的 XIB,以便您可以执行以下操作:
Create a Macro like so:
The name your XIBs so that you can do this:
您还可以使用 xcode 4 的功能。创建新项目时有一个选项,称为“通用应用程序”。
基于此模板的应用程序使用上述分离。
您将获得 iPhone 和 iPad 的文件夹,其中包含视图。
You also could use the abilities of xcode 4. There is an option while creating new projects which is called "universal app".
Apps based upon this template use the mentioned separation.
You get folders for iphone and ipad with views in it.
如果您有现有的应用程序,则可以选择
目标-->升级 iPad/iPhone
之后添加代码以检查应用程序是否在 iPad 或 iPhone 中运行。
If you have existing app then there is option in
Targets-->upgrade for iPad/iPhone
After that add code to check whether application is running in iPad or iPhone.