如何为同一项目中的不同目标设置不同的AppDelegate? [英] How to set different AppDelegate for different targets within same project?
问题描述
我对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)?
推荐答案
是的,您可以根据目标创建两个不同的对象,并进行以下更改:
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!