AppDelegate,RootViewController和UIApplication之间有什么关系? [英] What is the relationship between AppDelegate, RootViewController, and UIApplication?

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

问题描述

我试图弄清楚appdelegate,RootViewControoler和UIApplication之间的关系。以下是我到目前为止所得到的:


启动应用程序时,main.m将被加载。



从此处开始加载您的MainWindow.xib。



在您的MainWindow.xib中,您的文件所有者属于UIApplication类型。



您将UIApplication的委托设置为AppDelegate。



在AppDelegate的源代码中,您可以设置RootViewController成为第一个显示的视图。


这是对的吗?是什么促使AppDelegate最初运行它的

   - (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { } 

方法?

解决方案

当Objective-C应用程序启动时,它首先运行名为main()的函数。它不必在文件main.m中,但这就是Xcode向导设置的方式。



在向导生成的main()函数中,有这一行:

  int retVal = UIApplicationMain(argc,argv,nil,nil); 

这就是启动构成整个应用程序的UIKit框架的原因。在UIApplicationMain中,创建了UIApplication类型的对象。应用程序启动时UIApplication所做的部分工作是调用UIApplication类的委托成员上的applicationDidFinishLaunchingWithOptions方法。此委托在MainWindow.xib文件中设置为ProjectAppDelegate类的实例,该类是NSObject的子类,符合UIApplicationDelegate协议。


什么提示AppDelegate最初
运行它...


因为在你的MainWindow.xib文件中你连接了(以及项目向导实际上是连接)文件的所有者(它是UIApplication对象)对.xib文件中UIApplicationDelegate对象的委托出口,并且UIApplicationDelegate的类设置为应用程序的UIApplicationDelegate子类。



MainWindow.xib并没有什么神奇之处,它可以被称为Foo.xib,重要的是你的Info.plist文件中的属性被称为主nib文件基本名称是MainWindow。尝试将MainWindow.xib重命名为Foo.xib,并将Info.plist中的主nib文件基本名称更改为Foo,您将看到它仍然有效。



<编辑:关于RootController的更多信息



同样,所谓的RootController并没有什么神奇之处。这只是Xcode新项目向导为您创建的UIViewController子类的名称。



该向导将代码放在项目中,用于两个类:ProjectAppDelegate和ProjectViewController。 ProjectAppDelegate类包含两个出口成员:

  IBOutlet UIWindow *窗口; 
IBOutlet ProjectViewController * viewController;在MainWindow.xib文件中,

,放置了UIWindow和ProjectViewController的实例,并连接到ProjectAppDelegate上面的插座。



在ProjectAppDelegate类中,这些代码在屏幕上显示了什么:

   - (BOOL)应用程序:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//在应用程序启动后覆盖自定义点。

//将视图控制器的视图添加到窗口并显示。
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];

返回YES;
}

同样,没有什么真正的神奇之处:项目向导创建了添加你的代码rootViewController查看窗口的视图,并使窗口可见。您的根视图控制器是在.xib文件中创建的,并连接到ProjectAppDelegate插座。



尝试完全自己创建应用程序而不使用向导中的任何文件是非常有益的。您将学习很多关于.xib文件的工作原理以及它们与代码对象的关系。


I am trying to figure out the relationship between the appdelegate, RootViewControoler, and UIApplication. Here is what I kinda have figured out so far:

When starting your application up, main.m gets loaded.

From here, your MainWindow.xib gets loaded.

In your MainWindow.xib, your File's Owner is of type UIApplication.

You set your UIApplication's delegate to your AppDelegate.

In your AppDelegate's source code, you can set your RootViewController to be the first view shown.

Is this right? What prompts AppDelegate to initially run it's

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { }

method?

解决方案

When an Objective-C application starts, it starts by running the function named main(). It doesn't have to be in the file "main.m" but that's how the Xcode wizard sets things up.

Inside the wizard-produced main() function, there is this line:

int retVal = UIApplicationMain(argc, argv, nil, nil);

That is what starts the "UIKit" framework that makes up the entire application. Inside UIApplicationMain, an object of type UIApplication is created. And part of what UIApplication does when the application starts is call the applicationDidFinishLaunchingWithOptions method on the delegate member of the UIApplication class. This delegate is set up in the MainWindow.xib file to be an instance of your ProjectAppDelegate class, a subclass of NSObject that conforms to the UIApplicationDelegate protocol.

What prompts AppDelegate to initially run it's ...

Because in your MainWindow.xib file you have connected (well the project wizard did the connection actually) the File's Owner (which is the UIApplication object)'s "delegate" outlet to the UIApplicationDelegate object in the the .xib file, and the class of the UIApplicationDelegate is set to your app's UIApplicationDelegate subclass.

And there's nothing magic about "MainWindow.xib", it could be called "Foo.xib", what's important is that the property in your Info.plist file called "Main nib file base name" is "MainWindow". Trying renaming MainWindow.xib to Foo.xib and changing the "Main nib file base name" in your Info.plist to "Foo" and you'll see it still works.

EDIT: more about RootController

Again, there's nothing magic about the so-called "RootController". This is just the name of the UIViewController subclass created for you by the Xcode new project wizard.

The wizard places code in the project for two classes: ProjectAppDelegate and ProjectViewController. The ProjectAppDelegate class contains two outlet members:

IBOutlet UIWindow *window;
IBOutlet ProjectViewController *viewController;

in the MainWindow.xib file, instances of both UIWindow and ProjectViewController are placed, and hooked up to the above outlets in ProjectAppDelegate.

What gets your stuff up on the screen is this code in your ProjectAppDelegate class:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

Again, nothing really magic about this: the project wizard created code that adds your "root" ViewController's view to the window's view, and makes the window visible. Your "root" view controller was create in the .xib file, and hooked up to the ProjectAppDelegate outlet.

It is very instructive to try to create an application entirely by yourself without using any of the files from the wizard. You'll learn a lot about how .xib files work and how they relate to code objects.

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

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