在Universal ios应用程序中有条理地导入头文件

时间:2011-12-16 05:01:20

标签: iphone objective-c

在我的Universal ios应用程序中,我有两个头文件,例如FrameRates_iPad.hFrameRates_iPhone.h,我需要将其导入到viewcontroller类中,具体取决于应用程序正在构建的当前设备,请帮助我实现此目的

2 个答案:

答案 0 :(得分:0)

我喜欢为我的通用应用程序使用1个控制器。无需使用FrameRates_iPad.hFrameRates_iPhone.h并编写相同的代码两次!以下是我为iPad和iPhone特定代码使用1个控制器的方法。

您可以查看my post on how I use Macros

检查运行代码的设备的一种方法是使用UI_USER_INTERFACE_IDIOM()UI_USER_INTERFACE_IDIOM返回当前设备支持的界面惯用语,可在iOS 3.2 +。

中使用
#define IDIOM    UI_USER_INTERFACE_IDIOM()
#define IPAD     UIUserInterfaceIdiomPad   /* iPad, obviously               */
#define IPHONE   UIUserInterfaceIdiomPhone /* iPhone, to include iPod touch */

使用接口惯用法确定要运行的代码的示例:

此代码表示如果设备是iPad,则它可以自动旋转,否则,如果设备是iPhone或同等设备,则支持的唯一方向是Portrait。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if ( IDIOM == IPAD ) {
        return YES;
    }else{
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    return YES;
}

关于何时使用它的另一个例子:

为按钮或具有框架属性的任何其他对象设置特定大小。

CGRect frame;
if ( IDIOM == IPAD ) {
    frame = CGRectMake(tableView.frame.size.width-105,5,30,30);
} else {
    frame = CGRectMake(tableView.frame.size.width-60,5,30,30);
}

UIButton *aButton = [[UIButton alloc] initWithFrame: frame ];

答案 1 :(得分:0)

您可以在xcode构建设置中为 iPad_target 设置预处理器宏 IS_IPAD = 1 ,然后:

#ifdef IS_IPAD
#import "FrameRates_iPad.h"
#endif

#ifndef IS_IPAD
#import "FrameRates_iPhone.h"
#endif
相关问题