How to subclass UIApplication?

前端 未结 4 1325
清酒与你
清酒与你 2020-11-30 10:27

The iPhone Reference Libary - UIApplication says I can subclass UIApplication, but if I try this I will get an exception:

*** Terminating app due to uncaught         


        
相关标签:
4条回答
  • 2020-11-30 10:41

    I know this is an old one and accepted answer is o.k., but to make it clear...

    Let's say you have:

    UIApplication subclassed as MyApplication

    UIApplicationDelegate subclassed as MyAppDelegate

    Then your main.m would look like:

    #import <UIKit/UIKit.h>
    
    #import "MyApplication.h"
    #import "MyAppDelegate.h"
    
    int main(int argc, char * argv[])
    {
        @autoreleasepool
        {
            return UIApplicationMain(argc,
                                     argv,
                                     NSStringFromClass([MyApplication class]),
                                     NSStringFromClass([MyAppDelegate class]));                
        }
    }
    

    In case you did not subclass UIApplication or UIApplicationDelegate you simply pass nil to the UIApplicationMain as the respective parameter.

    0 讨论(0)
  • 2020-11-30 10:45

    In your app's info.plist, make sure you change the NSPrincipalClass key to the name of your subclass. This'll make Cocoa instantiate the correct class when the applications loads - you shouldn't have to do anything other than that to get your subclass working.

    0 讨论(0)
  • 2020-11-30 10:55

    There can only be one UIApplication instance in an application, you should not initialize the one new application object, use the static method of the UIApplication:+ (UIApplication *)sharedApplication.

    0 讨论(0)
  • 2020-11-30 11:01

    Did you pass the name of your subclass to UIApplicationMain? Let's assume you have

    @interface MyUIApp : UIApplication 
    ...
    

    then in main() you should do:

    NSString* appClass = @"MyUIApp";
    NSString* delegateClass = nil;
    int retVal = UIApplicationMain(argc, argv, appClass, delegateClass);
    
    0 讨论(0)
提交回复
热议问题