Main中UIApplicationMain之后的Objective C执行顺序是什么?

发布于 2024-10-18 20:41:43 字数 422 浏览 5 评论 0原文

有人可以解释一下 iOS 应用程序中的执行控制流程吗?我知道 UIApplicationMain 首先从 main 调用。然后呢?我定义的方法和main有什么关系?都是事件驱动的还是可以有某种结构化的顺序?

我并不是要如此含糊,我只是需要知道从哪里开始。也许我看待这个问题的方式是错误的。

例如,在 C++ 中我会做类似的事情:

#include "myMethods.h"
int main (int argc, char * const argv[]) {

   Method1(); // Initialization
   Method2(); // Opening views and options
   Method3(); // Meat of the program

   return 0;
}

提前致谢。

Could someone please explain how control of execution flows in an iOS application? I know that UIApplicationMain is called first from main. Then what? What is the relationship between my defined methods and main? Is it all event-driven or can there be some structured sequence?

I don't mean to be so vague, I just need to know where to start. Perhaps I'm looking at this in the wrong way.

For example, in C++ I would do something like:

#include "myMethods.h"
int main (int argc, char * const argv[]) {

   Method1(); // Initialization
   Method2(); // Opening views and options
   Method3(); // Meat of the program

   return 0;
}

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独夜无伴 2024-10-25 20:41:43

因此,正如您所提到的,main.m 中的 main() 函数是起点,然后调用 UIApplicationMain()。如果您检查文档,您会看到 UIApplicationMain 采用四个参数:

  • argc、
  • *argv[]、
  • *principalClassName
  • *delegateClassName。

其中前两个只是从 main() 传递的参数计数和变量列表。但第三个和第四个参数是指向 NSStrings 的指针。第三个参数指定哪个类应该是 UIApplication。除非您打算子类化 UIApplication,否则请为第三个参数指定 nil。第四个参数指定哪个类应该是 UIApplication 的委托类,它将响应 UIApplicationDelegate 协议。您不必直接处理它,因为它包含在所有 Xcode 模板中:

int main(int argc, char *argv[])
{
    @autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

不要让 NSStringFromClass([AppDelegate class])) 位困扰您。这只是指定第四个参数的一种奇特方式,以便在您稍后更改 AppDelegate.m 的名称时调用正确的委托。

UIApplication 启动 主事件循环并调用-application:didFinishLaunchingWithOptions:,这是其委托必须处理的方法之一。查看 AppDelegate.m,您会找到此方法的一些模板代码。您可以在此处开始自定义,创建在创建 UIWindowUIView 的其他实例之前需要就位的内容:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

现在是应用程序窗口和根视图控制器已定义,并且应用程序已关闭并正在运行。

所有这些以及更多内容都在这里得到了很好的解释: http://oleb.net/blog/2012/02/app-launch-sequence-ios-revisited/

So, as you mentioned, the main() function in main.m is the starting point, which then calls UIApplicationMain(). If you check the docs, you'll see that UIApplicationMain takes four arguments:

  • argc,
  • *argv[],
  • *principalClassName
  • *delegateClassName.

The first two of those are just the argument count and variable list passed from main(). But the third and fourth arguments are pointers to NSStrings. The third argument specifies which class should be UIApplication. Unless you intend on subclassing UIApplication, you specify nil for the third argument. The fourth argument specifies which class should be UIApplication's delegate class, which will respond to anything specified in the UIApplicationDelegate Protocol. You don't have to muck with this directly, as it's included in all of the Xcode templates:

int main(int argc, char *argv[])
{
    @autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

Don't let the NSStringFromClass([AppDelegate class])) bit throw you. That's just a fancy way of specifying the fourth argument so that the right delegate gets called should you later change the name of AppDelegate.m.

UIApplication starts the main event loop and calls -application:didFinishLaunchingWithOptions:, one of the methods its delegate has to handle. Take a look at AppDelegate.m, and you'll find some template code for this method. This is where you can start customizing, creating things that need to be in place before the UIWindow and other instances of UIView get created:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

So, now the application window and root view controller are defined, and the app is off and running.

All of this, and quite a bit more, is excellently explained here: http://oleb.net/blog/2012/02/app-launch-sequence-ios-revisited/

我一直都在从未离去 2024-10-25 20:41:43

正如您所说, UIApplicationMain 在系统中创建应用程序执行。在应用程序加载过程所做的事情中,我假设您对与特定应用程序相关的内容感兴趣。我还假设了一个典型的案例,Xcode 提供的许多项目模板中都有说明。

应用程序加载过程查看应用程序的信息属性列表。它在那里找到“主 nib 文件基本名称”,并且应用程序的 UIApplication 实例从应用程序包中加载相应的 nib 文件。此 nib 文件指定一个应用程序委托类,并指示将该类的实例连接到 UIApplication 实例的委托属性。

根据主 nib 文件,还可以创建和连接其他对象,例如应用程序的窗口、主视图控制器等。

现在加载序列结束,一切都是事件驱动的,从应用程序委托开始类来获取著名的 -applicationDidFinishLaunching: 消息。

As you said UIApplicationMain creates an application execution in the system. Among the stuff the application loading process does, I assume you are interested in what is relevant to a specific application. Also I assume a typical case, which is illustrated in many project templates that Xcode provides.

The application loading process looks into the application's information property list. There it finds 'Main nib file base name', and the UIApplication instance of your application loads the corresponding nib file from the application bundle. This nib file specifies an application delegate class, and tells to connect an instance of the class to the delegate property of your UIApplication instance.

Depending on the main nib file, other objects may be created and connected as well, for example, the application's window, the main view controller, etc.

Now the loading sequence ends, and everything is all event-driven, starting from your application delegate class to get the famous -applicationDidFinishLaunching: message.

御守 2024-10-25 20:41:43

来自 Apple 文档 -

应用程序生命周期构成了应用程序启动和终止之间发生的事件序列。在 iOS 中,用户通过点击主屏幕上的图标来启动您的应用程序。点击发生后不久,系统会显示一些过渡图形,并通过调用其主函数来启动您的应用程序。从此时起,大部分初始化工作都交给 UIKit,它加载应用程序的主 nib 文件并准备好事件循环。

应用程序生命周期

From Apple Documents -

The application life cycle constitutes the sequence of events that occurs between the launch and termination of your application. In iOS, the user launches your application by tapping its icon on the Home screen. Shortly after the tap occurs, the system displays some transitional graphics and proceeds to launch your application by calling its main function. From this point on, the bulk of the initialization work is handed over to UIKit, which loads the application’s main nib file and readies the event loop.

Application Life Cycle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文