xcode 如何更改应用程序委托类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9061239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:08:54  来源:igfitidea点击:

How to change the Application Delegate class?

iphoneiosxcode

提问by Oded Regev

In every iOS app there is an App Delegate class, meaning one of the classes in the app has to implement the delegate methods for the application events such as didFinishLaunching:and so forth. Usually the class name contains "AppDelegate".

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

The App Delegate class is a subclass of UIApplicationDelegateon iOS or NSApplicationDelegateon the Mac.

App Delegate 类是UIApplicationDelegateiOS 或NSApplicationDelegateMac 上的子类。

class AppDelegate: UIApplicationDelegate {

}

Let's say I want to implement the App Delegate methods in a different class than the original one Xcode created and named for me. How can I do that?

假设我想在与最初为我创建和命名的 Xcode 不同的类中实现 App Delegate 方法。我怎样才能做到这一点?

回答by Amresh Kumar

You can do the same by modifying the parameters to the UIApplicationMainfunction present in the main.mfile:

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

UIApplicationMain(argc,argv,nil,nil);

The last parameter takes the name of the class which is implementing the UIApplicationDelegateprotocol.

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

So the default implementation looks something like:

所以默认实现看起来像:

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

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;

回答by Sauvik Dolui

I achieved this in Swift by changing the AppDelegate's default template implementation:

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

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

replace with:

用。。。来代替:

import UIKit

@UIApplicationMain
class MyAppAppDelegate: UIResponder, UIApplicationDelegate {

See the new class name.

查看新的类名。