1

I have my ipad code created programmatically not using nib.I converted my ipad app to universal app as follows:

Choose the project target(the blue one on the sidebar). Summary -> iOS Application Target -> set Devices to Universal.

Now I want to launch different app delegate for iphone and different app delegate for ipad.There is already one app delegate for ipad, now I want to create different app delegate for iphone so that in main.m I can launch different app delegate based on device(ipad & iphone).So my question is, can I create different app delegate, if yes then how?

4

3 に答える 3

2

プロジェクトで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はそうすべきではありません。

代わりに、Apple が行うように、アプリ デリゲートで異なるビュー コントローラーまたは異なる 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
于 2012-06-21T10:00:38.543 に答える
0

アプリの XIB を介してこれを行うことができるはずです。デフォルトでは、AppDelegate はMainWindow.xib(またはメインの XIB ファイルの名前) のファイル所有者とデリゲート アウトレットの間の接続によって割り当てられます。コンバーターを使用した場合は、MainWindow~ipad.xib現在同じデリゲートを指している同様のアウトレットが必要です。それらを異なるものにしたい場合は、新しい AppDelegate サブクラスを作成し、それを~ipadXIB のバージョンのアウトレットに割り当てます。これは、手動で行う必要なしに UIApplicationMain を呼び出すのと同等である必要があります。

于 2012-06-21T10:05:34.380 に答える
0

そこの「appDelegateName」変数について心配する必要さえありません。次に示すように return を呼び出すだけです。

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

          if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
              return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate_iPad class]));
          } else {
              return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate_iPhone class]));
          }

       }
   }
于 2013-04-29T23:53:53.823 に答える