How to change the App-Delegate class

Each iOS application has a delegate application class, that is, one of the classes in the application must implement delegation methods for application events such as didFinishLaunching, etc. (typically, the class name contains "appDelegate").

My question is: let them say that I want to implement the app-delegate methods in a different class than the original xcode for me. How can i do this?

+5
source share
2 answers

You can do the same by changing the settings to

UIApplicationMain(argc,argv,nil,nil);
present in the main.m file. The last parameter takes the name of the class which is implementing the UIApplicationDelegate protocol.
So the default implementation looks something like

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
So after modifying it will be something like

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([< Your class name will go here > class]));
[pool release];
return retVal;
+7
source

Swift 3.0

I achieved the same by changing the AppDelegatedefault template implementation in Swift 3.0

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
         .....

with

import UIKit

@UIApplicationMain
class MyAppAppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

See the new class name.

0
source

All Articles