如何为同一项目中的不同目标设置不同的

如何为同一项目中的不同目标设置不同的

本文介绍了如何为同一项目中的不同目标设置不同的 AppDelegate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Xcode 中的目标概念非常陌生.我已经按照这个教程学习创建两个目标在同一个项目中.我只想知道如何让目标 A 使用 AppDelegateA.swift 作为其指定的应用程序委托,而目标 B 使用 AppDelegateB.swift 作为其指定的应用程序委托.因为在教程中,它实际上教了如何从同一个 AppDelegate 制作两个应用程序.但我制作了两个(几乎)完全不同的应用,它们共享大量资源和库.

I'm very very new to the target concept in Xcode. I have followed this tutorial to learn to create two targets in the same project. I just want to know how to make target A use AppDelegateA.swift as its designated app delegate, and target B use AppDelegateB.swift as its designated app delegate. Because on the tutorial, it actually teaches how to make two apps from the same AppDelegate. But I make two (almost) completely different apps, that share a lot of resources and libraries.

当我们讨论这个主题时,我是否也可以让目标 A 使用名为 Main 的故事板,而目标 B 也使用名为 Main 的故事板,但是它们实际上是不同的故事板(但放在同一个项目中)?

And while we're on the subject, can I also have target A use a storyboard called Main, and target B also use a storyboard called Main, but they are actually a different storyboard (but put together inside the same project)?

推荐答案

是的,您可以根据目标创建 2 个不同的,进行以下更改:

Yes you can create 2 different based upon the target make following changes:

在项目中

main.m

你可以做类似的事情

int main(int argc, char *argv[])
{
    @autoreleasepool {

        NSString *appDelegateName;
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
            appDelegateName =  NSStringFromClass([AppDelegateIPhone class]);
        } else {
            appDelegateName =  NSStringFromClass([AppDelegateIPad class]);
        }
        return UIApplicationMain(argc, argv, nil, appDelegateName);
    }
}

但是 IMO 你不应该这样做.

But IMO you should not do it.

而是像苹果那样做,在应用程序委托中加载不同的视图控制器或不同的 XIB.

Instead doi it as apple does it to, in app delegate load different view controllers or different XIBs.

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        // Override point for customization after application launch.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
        } else {
            self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
        }
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        return YES;
    }

@end

这篇关于如何为同一项目中的不同目标设置不同的 AppDelegate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 18:47