【问题标题】:How to change the Application Delegate class?如何更改应用程序委托类?
【发布时间】:2012-01-30 08:39:48
【问题描述】:

在每个 iOS 应用程序中都有一个 App Delegate 类,这意味着应用程序中的一个类必须实现应用程序事件的委托方法,例如 didFinishLaunching: 等等。通常类名中包含“AppDelegate”。

App Delegate 类是 iOS 上 UIApplicationDelegate 或 Mac 上 NSApplicationDelegate 的子类。

class AppDelegate: UIApplicationDelegate {

}

假设我想在与为我创建和命名的原始 Xcode 不同的类中实现 App Delegate 方法。我该怎么做?

【问题讨论】:

    标签: iphone ios xcode


    【解决方案1】:

    您可以通过修改 ma​​in.m 文件中的 UIApplicationMain 函数的参数来执行相同操作:

    UIApplicationMain(argc,argv,nil,nil);
    

    最后一个参数采用实现 UIApplicationDelegate 协议的类的名称。

    所以默认实现看起来像:

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
    

    修改后会是这样的:

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

    【讨论】:

    • 我不知道它是否仍然相关,但我在我的项目中没有找到任何 main.m 文件
    【解决方案2】:

    我通过更改 AppDelegate 的默认模板实现在 Swift 中实现了这一点:

    import UIKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    

    替换为:

    import UIKit
    
    @UIApplicationMain
    class MyAppAppDelegate: UIResponder, UIApplicationDelegate {
    

    查看新的类名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 2012-11-28
      相关资源
      最近更新 更多