我试图理解 @UIApplicationMain
的自动魔力以及如何用 Java 可视化 iOS 应用程序的启动:
public class UIApplication extends UIResponder implements Runnable {
final UIApplicationDelegate appDel;
public UIApplication(UIApplicationDelegate appDel) {
this.appDel = appDel;
}
public static void main(String[] args) {
try {
UIApplication app = new UIApplication(new AppDelegate());
handThisReferenceToOperatingSystem(app);
iOSdoesSomethingLikeThis(new Thread(app).start());
} catch(Exception e) { e.printStackTrace(); }
}
public void run() {
// chill-out and wait for iOS to invoke methods in UIResponder class.
// The UIResponder methods invoke my custom methods in AppDelegate.
}
public static class AppDelegate implements UIApplicationDelegate {
public void application(Object UIApplication) { // app specific behaviour
}
public void applicationWillResignActive(Object UIApplication) { // app specific behaviour
}
public void applicationDidEnterBackground(Object UIApplication) { // app specific behaviour
}
public void applicationWillEnterForeground(Object UIApplication) { // app specific behaviour
}
public void applicationDidBecomeActive(Object UIApplication) { // app specific behaviour
}
public void applicationWillTerminate(Object UIApplication) { // app specific behaviour
}
// maybe more methods from the UIApplicationDelegate
}
public interface UIApplicationDelegate {
void application(Object UIApplication);
void applicationWillResignActive(Object UIApplication);
void applicationDidEnterBackground(Object UIApplication);
void applicationWillEnterForeground(Object UIApplication);
void applicationDidBecomeActive(Object UIApplication);
void applicationWillTerminate(Object UIApplication);
// maybe some more methods ....
}
}
public class UIResponder {
void fingerSwipe() { // default implementation
}
void verticalMotion() { // default implementation
}
// more methods iOS might invoke
}
基本上,将 @UIApplicationMain
属性应用于 AppDelegate 类会使所有其他代码消失,对吗?
最佳答案
看看这个答案:https://stackoverflow.com/a/24516329/335974
要点是它生成 Objective-C 项目中的 main.m
文件:
int main(int argc, char *argv[]) {
@autoreleasepool {
NSString *appDelegateClassName = @"AppDelegate";
return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}
}
因此,您的 Java 代码看起来与所发生的情况大致相同。
关于java - 这个 Java 是否很好地近似了 @UIApplicationMain 在 Swift 中的工作方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38884123/