iOS - 条件编译(xcode) [英] iOS - conditional compilation (xcode)

查看:150
本文介绍了iOS - 条件编译(xcode)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



从Apple文档:
在几乎所有情况下,您都将想要定义一个新的视图控制器类来管理你的应用程序接口的iPad版本,特别是如果该接口与你的iPhone接口不同,你可以使用条件编译来协调不同视图控制器的创建。



但是他们没有给出任何关于什么条件编译的例子或细节。任何人都可以举个例子吗?



编辑:
我已经尝试在iPad目标中定义以下C Flags:-D USE_IPAD,-DUSE_IPAD。
无论哪种方式,此代码总是导致IPHONE记录:

  #ifdef USE_IPAD 

NSLog(@IPAD);

#else

NSLog(@IPHONE);

#endif

我缺少什么?

解决方案

另一种可能更适合条件编译的方法是直接的 if else 语句:

  if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad){
// iPad代码
} else {
// iPhone或iPod Touch代码
}

因为我发现它更容易维护和更少的线路噪声。


I have created an additional iPad target for what was originally an iphone app.

From the Apple docs: "In nearly all cases, you will want to define a new view controller class to manage the iPad version of your application interface, especially if that interface is at all different from your iPhone interface. You can use conditional compilation to coordinate the creation of the different view controllers."

But they don't give any example or detail on what conditional compilation is. Can anyone give an example? And where would I do this?

EDIT: I have tried defining the following C Flags in the iPad target: -D USE_IPAD, -DUSE_IPAD. Either way, this code always results in IPHONE getting logged:

#ifdef USE_IPAD

        NSLog(@"IPAD");

    #else

        NSLog(@"IPHONE");

    #endif

What am I missing?

解决方案

Another approach which may be preferable to conditional compilation is a straight-forward if else statement:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    // iPad code
} else {
    // iPhone or iPod Touch code
}

I prefer this over conditional compilation as I find it easier to maintain and less line noise.

这篇关于iOS - 条件编译(xcode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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