简体   繁体   English

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

[英]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.

相关问题 如何在应用程序委托中访问我的班级的变量? - How to access a var of my class in the application delegate? 在应用程序委托 class 中重置属性 - Reset property in application delegate class 如何将委托更改为NSURLSession - How to change delegate to NSURLSession 如何在基于窗口的iPhone应用程序中从应用程序委托类进行导航。 如何在应用程序委托类中编写pushViewcontroller方法 - How to navigate from app delegate class in window based iPhone application. How to write pushViewcontroller method in app delegate class 如何在应用程序启动时从应用程序委托加载不同的视图控制器类(即从应用程序委托) - How to load different view controller class from app delegate at the time of application launch(i.e from app delegate) iPhone-在泛型类中引用应用程序委托 - iPhone - Referencing the application delegate within a generic class 声音类中的委托人如何使控制器类中的按钮图像发生变化? - How can a delegate in a sound class cause a change of a button image in a controller class? 如何使一个班级成为另一个的代表? - How to make a class as a delegate for another? 如何使用应用程序委托保存状态 - How to save states using an application delegate 如何从一个应用程序委托处理多视图? - how to handle multipleViews from one application delegate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM