无需Interface Builder即可制作通用应用 [英] Making universal apps without Interface Builder

查看:76
本文介绍了无需Interface Builder即可制作通用应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XCode 4.2,iOS 5,并希望使用Interface Builder创建一个通用应用程序 not ,如本文所述,不使用MainWindow.xib

I'm using XCode 4.2, with iOS 5, and would like to make a universal app, not using Interface Builder, as outlined in this post, not using a MainWindow.xib.

我还想使用Kotan Code的发布

I would also like to separate my iPhone and iPad code, using the technique outlined in Kotan Code's post.

所以我有主 appdelegate ,和 appdelegate_iPhone 以及从中继承的 appdelegate_iPad

So I have the main appdelegate, and appdelegate_iPhone and an appdelegate_iPad that inherit from it.

返回在XCode 4.0中(以及在Kotan的帖子中),使用了相应的appdelegate,因为有一个 MainWindow_iPhone.xib (或 MainWindow_iPad.xib )与之相关联。如果我没有使用.xibs,我如何以编程方式转到正确的appdelegate?

Back in XCode 4.0 (and in Kotan's post), the appropriate appdelegate was used because there was a MainWindow_iPhone.xib (or MainWindow_iPad.xib) associated with it. If I'm not using .xibs, how do I programatically go to the correct appdelegate?

(我希望解决方案不涉及 if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad){} etc)

(I'm hoping the solution doesn't involve if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {} etc)

推荐答案

看看你的 main.c 文件,它应该包含这样的语句:

Have a look at your main.c file, it should contain a statement like this:

int retVal = UIApplicationMain(argc, argv, nil, nil);

其中 UIApplicationMain 定义为:

int UIApplicationMain (
  int argc,
  char *argv[],
  NSString *principalClassName,
  NSString *delegateClassName
);

所以,你只需执行:

int retVal = UIApplicationMain(argc, argv, nil, <YOUR_DELEGATE_CLASS_NAME>);

并且您的程序将使用该委托类而不是笔尖中定义的委托类。

and you program will use that delegate class instead of the one defined in the nib.

还可以考虑编辑info.plist文件,在那里你应该删除你在那里找到的主要nib文件的所有条目,否则如果从项目中删除x​​ib文件,你的程序就会崩溃。

Also consider editing the info.plist file, where you should remove all entries about the main nib files you find there, otherwise your program will crash if you remove the xib files from the project.

编辑:我很晚才意识到你也在询问如何区分iPad和iPhone ...并希望这不涉及检查 UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad

I realized late that you are also asking about how to differentiate an iPad from an iPhone... and hoping this does not involve checking if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad.

AFAIK, UI_USER_INTERFACE_IDIOM 是正式方式要做到这一点。实际上,如果您检查该宏的定义,您将看到它基于 UIDevice 类,该类返回有关您运行程序的设备的信息。所以,我没有看到任何不好的内容。

AFAIK, UI_USER_INTERFACE_IDIOM is the official way to do that. Indeed, if you check the definition of that macro you will see it is based on a UIDevice class that returns information about the device where you program runs. So, I don't see anything bad in it.

另一种方法可能是使用 UIDevice 本身,或者 UIDevice-Extension ,这是一个扩展 UIDevice

An alternative approach might be using UIDevice itself, or UIDevice-Extension, which is a framework extending UIDevice.

编辑2:

提问你在评论中询问:


1)如果有.xibs,我不需要指定 UI_USER_INTERFACE_IDIOM
(根据上面链接的Kotan的帖子)。我必须在nib和
之间选择这个 if 声明吗?

我想是这样;要么你在xib级别,要么以编程方式。

I think so; either you do it at xib level, or programmatically.


2)你建议我把 if
main.m 中跟踪设备的语句?这是真的,它在那里工作。请注意XCode 4.2中的更改:

2) Are you suggesting I put the if statement to track the device in main.m? It's true, it does work there. Note the change in XCode 4.2:



return UIApplicationMain(argc, argv, nil, 
                         NSStringFromClass([AppDelegate class]));

如果以编程方式执行此操作,则必须在需要的地方执行此操作。

If you do it programmatically, you have to do it where you need it.

可能,应用程序委托是应用程序的一部分,不依赖于设备,因此您不需要为iphone实例化一个,而为ipad实例化另一个。我指的是,如果你没有提供一个笔尖,你应该指定一个委托,它只是为你的应用程序定义一个高级入口点。

Likely, the app delegate is a part of the app that does not depend on the device, so you would not strictly need to instantiate one for the iphone and a different one for the ipad. What I was pointing to is that if you are not providing a nib, you should specify a delegate, which simply defines for your app a high-level entry-point.

- 应用程序:didFinishLaunchingWithOptions:(应用程序入口点)中,您应该创建UI,即创建那些你将在nib中定义的对象集(控制器,视图,等等; nib只是一种可视化创建和连接对象的机制;如果你不直观地进行,你可以通过编程方式进行操作)和连接他们;现在,这些对象中的一些确实依赖于设备(即视图),而其他对象则不依赖于设备。对于前者,您可以通过使用 UI_USER_INTERFACE_IDIOM 检查来决定实例化它们的类。

In your – application:didFinishLaunchingWithOptions: (the app entry point) you should then create your UI, that is creating that set of objects that you would otherwise define in a nib (controllers, views, whatever; a nib is just a mechanism to visually "create" and connect objects; if you don't do it visually, you do it programmatically) and connecting them; now, some of those objects do depend on the device (i.e., views), others not. For the former ones, you can decide on which class to instantiate them from by using the UI_USER_INTERFACE_IDIOM check.

或者,你可以有两个不同的委托类,一个创建iphone UI,另一个创建ipad UI;这也是一种非常合理的方法。它主要取决于您的应用程序的复杂程度以及您愿意接受的权衡。

Alternatively, you could have two different delegate classes, one that creates the iphone UI and the other the ipad UI; this is also a perfectly reasonable approach. It basically depends on the complexity of your app and what trade-offs you are willing to accept.

这是更多或我看待事情的方式少,为可能迂腐道歉,但我希望我能说清楚。

This is more or less how I see things, apologies for possibly being pedantic, but I hope I could make myself clear.

如果你有很多如果你的应用程序周围,你也可以定义一个装饰器功能(例如: decoratedClassNameFromGenericClassName:(NSString *),不要介意详细)隐藏在其中 UI_USER_INTERFACE_IDIOM 或将来可能出现的任何装饰需求......

If you have many ifs around your app, you could also define a "decorator" function (e.g.: decoratedClassNameFromGenericClassName:(NSString*), never mind the verbosity) to hide in it UI_USER_INTERFACE_IDIOM or whatever other "decoration" need might arise in the future...

这篇关于无需Interface Builder即可制作通用应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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