根据设备类型定义常量

时间:2014-07-28 10:48:06

标签: objective-c iphone constants extern

您好我正在构建一个Iphone应用程序,其中我定义了一些常量。我知道很多人都会问同样的问题,但我无法弄清楚。所以我的问题是这样的。我在一个文件中定义了我的常量值,例如constants.mconstants.h。我的constant.h看起来像是:

@interface Constants : NSObject
    //  UI CONSTANTS
    extern int FONT_SIZE_14;
 @end

我的constants.m看起来像:

@implementation Constants

    //  UI CONSTANTS.

    #if (IS_IPAD)
        int FONT_SIZE_14 = 18;
    #else
        int FONT_SIZE_14 = 14;
    #endif
@end

所以它工作正常没有任何错误,但它总是只需要值14.如果它在ipad上运行它不会取18值。我做错了什么。需要一些帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

如果你同时支持ipad和iphone,那么在运行之前你就不会知道该设备。

您可以使用以下内容:

int myFontSize() {
  switch (UI_USER_INTERFACE_IDIOM()) {
    case UIUserInterfaceIdiomPhone :
      return FONT_SIZE_14;
    case UIUserInterfaceIdiomPad :
      return FONT_SIZE_18;
    default :
  }
}

或者您可以使用定义常量:

#define MY_CONSTANT_Font ( (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? FONT_SIZE_14 : FONT_SIZE_18