Can I create App Delegate file in my project?

2020-06-28 09:01发布

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?

3条回答
Luminary・发光体
2楼-- · 2020-06-28 09:32

You don't even need to worry about the "appDelegateName" variable there. Just call return as shown here:

  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]));
          }

       }
   }
查看更多
Viruses.
3楼-- · 2020-06-28 09:45

You should be able to do this through the XIB for your App. By default, the AppDelegate is assigned by the connection between the MainWindow.xib (or whatever your main XIB file is called)'s File Owner and the delegate outlet. If you used the converter, then the MainWindow~ipad.xib should have a similar outlet, which also currently points to the same delegate. If you want them to be different, create a new AppDelegate subclass and assign it to the outlet in the ~ipad version of the XIB and it should be the equivalent of calling UIApplicationMain without having to do that manually.

查看更多
Animai°情兽
4楼-- · 2020-06-28 09:52

in the projects main.m

you could do something like

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);
    }
}

But IMO you should not do it.

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
查看更多
登录 后发表回答