[英]How to change the Application Delegate class?
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.在每个 iOS 应用程序中都有一个 App Delegate 类,这意味着应用程序中的一个类必须实现应用程序事件的委托方法,例如didFinishLaunching:
等等。 Usually the class name contains "AppDelegate".通常类名包含“AppDelegate”。
The App Delegate class is a subclass of UIApplicationDelegate
on iOS or NSApplicationDelegate
on the Mac. App Delegate 类是 iOS 上的UIApplicationDelegate
或 Mac 上的NSApplicationDelegate
的子类。
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.假设我想在与最初为我创建和命名的 Xcode 不同的类中实现 App Delegate 方法。 How can I do that?我怎样才能做到这一点?
You can do the same by modifying the parameters to the UIApplicationMain
function present in the main.m file:您可以通过修改main.m文件中UIApplicationMain
函数的参数来执行相同的操作:
UIApplicationMain(argc,argv,nil,nil);
The last parameter takes the name of the class which is implementing the UIApplicationDelegate protocol.最后一个参数采用实现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;
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.查看新的类名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.