我没有使用nib以编程方式创建ipad代码,我将ipad应用程序转换为通用应用程序,如下所示:

选择项目目标(侧栏上的蓝色目标)。摘要-> iOS
应用程序目标->将设备设置为通用。

现在我想为iphone启动不同的应用程序委托,为ipad启动不同的应用程序委托。对于ipad,已经有一个应用程序委托,现在我想为iphone创建不同的应用程序委托,以便在main.m中可以基于设备(ipad和iphone)。所以我的问题是,我可以创建其他应用程序委托吗?

最佳答案

在项目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);
    }
}

但海事组织你不应该这样做。

而是像苹果一样,在应用程序委托中加载不同的视图控制器或不同的XIB。
@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

关于objective-c - 我可以在项目中创建App Delegate文件吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11135685/

10-09 16:23