xcode 你的应用程序委托在哪里设置,谁初始化它的窗口和视图控制器属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7535605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is your Application Delegate set and who initializes its window and viewController properties?
提问by Gianni Costanzi
I have a very newbie question about IOS apps... If I create a new View Based Application called TestForStackOverflow, Xcode automatically creates code like this for the TestForStackOverflowAppDelegate.h:
我有一个关于 IOS 应用程序的新手问题......如果我创建一个名为 TestForStackOverflow 的基于视图的新应用程序,Xcode 会自动为 TestForStackOverflowAppDelegate.h 创建这样的代码:
@class TestForStackOverflowViewController;
@interface TestForStackOverflowAppDelegate : NSObject <UIApplicationDelegate>
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet TestForStackOverflowViewController *viewController;
@end
and the following in TestForStackOverflowAppDelegate.m:
以及 TestForStackOverflowAppDelegate.m 中的以下内容:
#import "TestForStackOverflowAppDelegate.h"
#import "TestForStackOverflowViewController.h"
@implementation TestForStackOverflowAppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
[...]
Here come my questions:
我的问题来了:
1) where is the TestForStackOverflowAppDelegate class set as delegate for the current application? Is it done "automagically"? I've seen that the main.m source file contains only the following code:
1) TestForStackOverflowAppDelegate 类在哪里设置为当前应用程序的委托?它是“自动”完成的吗?我已经看到 main.m 源文件只包含以下代码:
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
Shouldn't it set the application delegate class in the fourth parameter of the UIApplicationMain function invocation?
不应该在 UIApplicationMain 函数调用的第四个参数中设置应用程序委托类吗?
2) where are the window and viewController properties of TestForStackOverflowAppDelegate class being set?
2) TestForStackOverflowAppDelegate 类的 window 和 viewController 属性在哪里设置?
3) this may be trivial, but why do we have synthesize window = _window, without having an instance variable called _window in TestForStackOverflowAppDelegate interface? I've seen that you can declare @properties without having the corresponding iVars in the class interfaces (maybe they are automatically created by the compiler), but is it a good practice or should you always create the corresponding iVars in your classes?
3) 这可能是微不足道的,但是为什么我们有合成窗口 = _window,而在 TestForStackOverflowAppDelegate 接口中没有名为 _window 的实例变量?我已经看到您可以在类接口中没有相应的 iVars 的情况下声明 @properties(也许它们是由编译器自动创建的),但这是一个好习惯还是应该始终在您的类中创建相应的 iVars?
Excuse me for the very long message, I just hope I've not written a too obvious question since here in Italy is late night and I'm very tired.. but when these questions come into my head, I can't wait for the solution :)
对不起,很长的信息,我只是希望我没有写一个太明显的问题,因为在意大利这里是深夜,我很累..但是当这些问题出现在我的脑海中时,我等不及了解决方案 :)
采纳答案by bryanmac
How is the app delegate class loaded?
应用委托类是如何加载的?
In your -info.plist file there is a key named "Main nib file base name" and has a view something like MainWindow.xib. In that xib file, there's a file's owner proxy object and it has a delegate set to the app delegate class.
在您的 -info.plist 文件中有一个名为“Main nib file base name”的键,并且有一个类似于 MainWindow.xib 的视图。在那个 xib 文件中,有一个文件的所有者代理对象,它有一个设置为应用程序委托类的委托。
Open that XIB in the designer. Notice the File's Owner object at the top, context click on it and look at the delegate object. Now look at the objects in the design below the (XCode 4) line - you should see the app delegate object there.
在设计器中打开该 XIB。注意顶部的 File's Owner 对象,上下文单击它并查看委托对象。现在查看设计中 (XCode 4) 行下方的对象 - 您应该在那里看到应用程序委托对象。
Where is the window and viewController for the appDelegate set?
appDelegate 设置的 window 和 viewController 在哪里?
Look in the designer and context click on the app delegate object below the (XCode 4) line. The window and viewController target is tied there.
在设计器和上下文中查看 (XCode 4) 行下方的应用程序委托对象。window 和 viewController 目标绑定在那里。
_window iVar
_window iVar
It's automatically provided for you. Not clear to me whether it's inherited or generated by the compiler.
它会自动为您提供。我不清楚它是由编译器继承还是生成的。
Here's more on _ iVars: Why rename synthesized properties in iOS with leading underscores?
这里有更多关于 _ iVars: 为什么在 iOS 中用前导下划线重命名合成属性?
If you chose to do the equivalent in code:
如果您选择在代码中执行等效操作:
remove plist xib reference main.m: pass name of delegate
删除 plist xib 引用 main.m:传递委托名称
int main(int argc, char *argv[])
{
int retVal = UIApplicationMain(argc, argv, nil, @"HelloViewAppDelegate");
In app delegate:
在应用程序委托中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
CGRect screenBounds = [[UIScreen mainScreen] applicationFrame];
CGRect windowBounds = screenBounds;
windowBounds.origin.y = 0.0;
// init window
[self setWindow: [[UIWindow alloc] initWithFrame:screenBounds]];
// init view controller
_mainViewController = [[MainViewController alloc] init];
[[self window] addSubview:[_mainViewController view]];
[self.window makeKeyAndVisible];
return YES;
}
回答by Pierre-David Belanger
1) To be simple, when UIApplicationMain()
is called,
1)简单来说,什么时候UIApplicationMain()
被调用,
- you application .plist is found,
- the key "Main nib file base name" is looked up,
- this XIB is opened,
- The object that supports the
UIApplicationDelegate
protocol is instantiated and used as the application delegate
- 找到您的应用程序 .plist,
- 查找关键的“主笔尖文件基名”,
- 此 XIB 已打开,
- 支持
UIApplicationDelegate
协议的对象被实例化并作为应用程序委托
2) They are set in the XIB, you can view them if you
2)它们是在XIB中设置的,如果您愿意,您可以查看它们
- open the main XIB,
- select the AppDelegate object,
- open the Connections inspector for this object,
- look into the Outlets section
- 打开主XIB,
- 选择 AppDelegate 对象,
- 打开此对象的连接检查器,
- 查看奥特莱斯部分
3) You are right, they are created automatically by the compiler. It makes the code more readable by having less line, and more easy to refactor having only one place the property is declared.
3)你是对的,它们是由编译器自动创建的。它通过减少行使代码更具可读性,并且仅在一处声明属性的情况下更易于重构。
Hope it helps!
希望能帮助到你!