AppDelegate和main.m之间的关系 [英] Relationship between AppDelegate and main.m

查看:116
本文介绍了AppDelegate和main.m之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我完全是obj-c +可可的新手,所以这很明显,但这是这样的:

Ok, I'm totally new to obj-c + cocoa, so this is probably obvious, but here goes:

我一直从命令行移动应用程序到可可应用程序中,以学习如何在Xcode中使用Objective-C。我不太了解的一件事是AppDelegate的角色以及它如何连接到main.m

I've been moving from command line apps to cocoa apps in learning how to work with objective-c in Xcode. One thing I don't really understand is the role of the AppDelegate and how it connects to main.m

似乎您可以将整个程序放在appdelegate中,它可以正常运行,甚至不需要main.m,但反之则不需要,如果要制作可可应用程序,则必须至少具有appdelegate。

It seems like you could put your entire program in the appdelegate and it would run fine, and you don't even need main.m, but not the other way around, if you're making a cocoa app you have to at least have the appdelegate.

我已经做了很多php Web开发和命令行工具,所以我想我要找的是程序将首先执行的文件,目的是控制其余文件

I've done a lot of php web development and command-line tools, so I guess what I'm looking for is the file that the program will execute first and is intended to "control" the rest of them.

任何人都可以帮助我了解Cocoa程序中发生了什么,AppDelegate和main.m之间(或不与它们之间)如何相关以及程序的流程如何。

Can anyone help me understand what's going on in a Cocoa program, how AppDelegate and main.m are (or are not) related, and what the flow of the program is supposed to be?

推荐答案

许多面向对象系统(例如Cocoa)的关键功能是控制反转 ,这基本上意味着该框架正在运行所有程序,并且您编写的任何代码都受其控制。

A key feature of many object-oriented systems (such as Cocoa) is "inversion of control", which basically means that the framework is running everything, and any code you write is under its control.

因此,与PHP不同,您不必编写在启动时执行的代码。您要做的是为应用程序委托,控制器,视图和其他对象定义方法,并让框架在需要时调用这些方法。您将永远不会看到整个程序的整体控制流。

So, unlike PHP, you don't write the code that executes at startup. What you do is define methods for the app delegate, controllers, views, and other objects, and let the framework invoke those methods as it needs to do so. You will never see the overall "flow of control" throughout the program; you will only see it as control flows into your pieces of the program.

起初,这很容易造成混淆,因为您试图弄清楚如何欺骗框架进行调用您的代码可以按您期望的时间和顺序排列,但是从长远来看,它实际上使事情变得更容易,因为您可以信任该框架来为您处理很多事情。

This can be confusing at first, as you try to figure out how to trick the framework into calling your code at the times and in the order you expect, but in the long run it actually makes things easier, as you can trust the framework to take care of a lot of things for you.

在Cocoa应用程序中,该应用程序的许多逻辑实际上将存在于视图控制器中,而不是在应用程序委托中。应用程序委托通常负责启动和关闭的责任,但其他对象则负责启动和关闭之间的大部分工作。因此,请勿尝试将所有内容都挤入应用程序委托中。

In a Cocoa app, a lot of the logic of the app will actually be in view controllers, rather than in the app delegate. The app delegate generally handles startup and shutdown responsibilities, but other objects do most of the work between startup and shutdown. So don't try to squeeze everything into the app delegate.

这篇关于AppDelegate和main.m之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆