【问题标题】:#if to determine device type in .h xcode#if 确定 .h xcode 中的设备类型
【发布时间】:2012-05-24 06:54:50
【问题描述】:

我确信这可能非常容易(或者无法完成),但我似乎找不到任何东西。

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

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

#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

我不确定该使用什么,非常感谢任何帮助。

感谢您的宝贵时间!

【问题讨论】:

    标签: ios xcode if-statement preprocessor header-files


    【解决方案1】:

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

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

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

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

    #define deltaX (IS_IPAD? 78: 10)
    

    【讨论】:

      【解决方案2】:

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

      您应该在头文件中声明这些变量,然后在运行时根据UI_USER_INTERFACE_IDIOM() 的值设置它们。

      【讨论】:

      • Dooah..我不知道为什么我没有想到这一点。当然预处理器不会知道设备中的差异。我应该知道 :(
      【解决方案3】:

      你已经有了确定设备的代码,没关系。

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

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

      然后在你的类文件中:

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

      或者:

      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
      

      希望这会有所帮助!

      【讨论】:

      • 这是我的第二个选择,只是我的定义被使用了很多,所以我想说远离在我的 .m 中使用 if/else。
      猜你喜欢
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 2011-07-01
      • 1970-01-01
      • 2012-09-06
      相关资源
      最近更新 更多