#if 在 .h xcode 中确定设备类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8372031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:39:56  来源:igfitidea点击:

#if to determine device type in .h xcode

iosxcodeif-statementpreprocessorheader-files

提问by random

I am sure that this probably something extremely easy (or it can't be done), but I can't seem to find anything on it.

我确信这可能是一件非常简单的事情(或者它无法完成),但我似乎找不到任何关于它的东西。

In one of my classes .h file I need to determine if the app is running on an iPad or an iPhone. Then change the value of the #define accordingly.

在我的一个类 .h 文件中,我需要确定应用程序是在 iPad 还是 iPhone 上运行。然后相应地更改#define 的值。

Ideally I would this it would look something like this:

理想情况下,我希望它看起来像这样:

#if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone

#define deltaX 10.0
#define theda  15.0
#define threshHold 267.0

#endif

#if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad

#define deltaX 78.1
#define theda  67.2
#define threshHold 453.0

#endif

I am not sure what to use, any help would be very much appreciated.

我不确定要使用什么,任何帮助将不胜感激。

Thank you for your time!

感谢您的时间!

回答by bornbnid

A little late to the party, but figured I'd share what worked for me.

聚会有点晚了,但我想我会分享对我有用的东西。

A solution that has been working for me is to define IS_IPAD and IS_IPHONE somewhere like so

一个对我有用的解决方案是在这样的地方定义 IS_IPAD 和 IS_IPHONE

#define IS_IPAD   (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

Then when needing the other defines based on ipad/iphone, do something like this

然后当需要基于 ipad/iphone 的其他定义时,做这样的事情

#define deltaX (IS_IPAD? 78: 10)

回答by joerick

Sadly, you can't do this, as in a universal app the same code runs on iPhone as on iPad, so this decision must be made at run-time, not compile-time.

遗憾的是,您不能这样做,因为在通用应用程序中,iPhone 和 iPad 上运行的代码相同,因此必须在运行时而非编译时做出此决定。

You should declare these variables in a header file, and then set them at run time depending on the value of UI_USER_INTERFACE_IDIOM().

您应该在头文件中声明这些变量,然后在运行时根据UI_USER_INTERFACE_IDIOM().

回答by Luke

You already have the code to determine the device, so that's fine.

您已经有了确定设备的代码,所以没问题。

I'd create your defines as follows:

我会按如下方式创建您的定义:

#define padDeltaX 10.0
#define phoneDeltaX 78.1
... etc

Then in your class file:

然后在你的类文件中:

if (if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    // do iPhone processing with the variables
}
else
{
    // must be iPad
}

Alternatively:

或者:

float variableOne, variableTwo; // etc

if (if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    variableOne = phoneDeltaX;
    variableTwo = phoneTheta; // etc
}
else
{
    // must be iPad
    variableOne = padDeltaX;
    variableTwo = padTheta; // etc
}

// now do the shared processing with variableOne, variableTwo etc

Hope this helps!

希望这可以帮助!