0

Is it possible to have separate out AppDelegate.h for different targets: iPhone/iPad?

I have a default common main.m:

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

I have 2 targets setup correctly with an appdelegate.h/.m for each target in each iPhone/ iPad folder.

They header files are similar and it is complaining about Duplicate interface definition from the iPad appdelegate.h when I build the iPhone target. However, the reverse just builds ok.

How can I fix this?

4

3 回答 3

3

您应该转到每个目标的“构建阶段”,并确保每个目标都在“编译源”下包含适当的应用程序委托。如果您给这两个应用程序代表不同的类,您可能还想更改main.m为使用适当的类:

#import <UIKit/UIKit.h>

#import "IphoneAppDelegate.h"
#import "IpadAppDelegate.h"

int main(int argc, char * argv[])
{
    @autoreleasepool {
        if (NSClassFromString(@"IphoneAppDelegate"))
            return UIApplicationMain(argc, argv, nil, @"IphoneAppDelegate");
        else
            return UIApplicationMain(argc, argv, nil, @"IpadAppDelegate");
    }
}

就个人而言,我更喜欢有一个应用程序委托,并对 iPhone/iPad 进行条件检查,例如,

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
     // do iPad specific stuff
}

相对

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
     // do iPhone specific stuff
}

虽然它看起来很笨拙,但两个平台上的大部分应用程序代表将是相同的,并且它引入了代码维护问题以拥有两个应用程序代表。

于 2013-10-25T16:09:52.983 回答
0

我不确切知道 iPhone 和 iPad 的目标,但是

如果您在项目中有两个不同的构建目标,您可以通过简单地为不同的 AppDelegate 文件选择适当的目标成员资格来指定要包含在构建中的不同文件。

让两个 AppDelegate.m 用于不同的构建目标: - 首先,让您的 AppDelegate.m 只检查一个构建目标。- 其次,为了创建一个副本,去finder并寻找你原来的AppDelegate.m;创建子文件夹并将文件复制到那里。- 现在,在 Xcode 中创建引用:将文件拖到原始 AppDelegate.m 旁边的 Xcode 窗口中,选中“创建引用”并仅选择第二个构建目标。

此时,您的项目中应该有两个彼此相邻的 AppDelegate.m。

于 2014-02-16T20:46:16.680 回答
0

One way would be to add a "Preprocessor Macro" in the build settings of your target so, for example in your iPhone target you put "BUILD_FOR_IPHONE=1" and in your iPad "BUILD_FOR_IPHONE=0". Then you can add in your iPhone's AppDelegate.h the following

#if BUILD_FOR_IPHONE
<your normal .h contents>
#endif

and in your iPad's

#if !BUILD_FOR_IPHONE
....
#endif
于 2013-10-25T16:10:34.487 回答