您的Application Delegate设置在哪里以及谁初始化其window和viewController属性? [英] Where is your Application Delegate set and who initializes its window and viewController properties?

查看:144
本文介绍了您的Application Delegate设置在哪里以及谁初始化其window和viewController属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于IOS应用程序的新手问题...如果我创建一个名为TestForStackOverflow的新的基于视图的应用程序,Xcode会自动为TestForStackOverflowAppDelegate.h创建这样的代码:

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:

@class TestForStackOverflowViewController;

@interface TestForStackOverflowAppDelegate : NSObject <UIApplicationDelegate>

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet TestForStackOverflowViewController *viewController;

@end

以及TestForStackOverflowAppDelegate.m中的以下内容:

and the following in 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;
}

[...]

这是我的问题:

1)TestForStackOverflowAppDelegate类在哪里设置为当前应用程序的委托?它是自动完成的吗?我已经看到main.m源文件只包含
以下代码:

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:

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

不应该在UIApplicationMain的第四个参数中设置应用程序委托类函数调用?

Shouldn't it set the application delegate class in the fourth parameter of the UIApplicationMain function invocation?

2)是否设置了TestForStackOverflowAppDelegate类的window和viewController属性?

2) where are the window and viewController properties of TestForStackOverflowAppDelegate class being set?

3)这可能是微不足道的,但为什么我们合成了window = _window,而没有在TestForStackOverflowAppDelegate接口中有一个名为_window的实例变量?我已经看到你可以在类接口中没有相应的iVar声明@properties(也许它们是由编译器自动创建的),但这是一个好习惯还是你总是应该在你的类中创建相应的iVars?

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?

对不起,我只是希望我没有写过一个太明显的问题,因为在意大利这里是深夜,我很累......但是这些问题进入我的脑海,我迫不及待地想要解决方案:)

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 :)

推荐答案

应用如何委托类加载?

在-info.plist文件中有一个名为主nib文件基本名称的键,其视图类似于MainWindow.xib 。在该xib文件中,有一个文件的所有者代理对象,并且它具有设置为app delegate类的委托。

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.

在设计器中打开该XIB。注意顶部的File的Owner对象,上下文单击它并查看委托对象。现在看一下(XCode 4)行下面的设计中的对象 - 你应该在那里看到app delegate对象。

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.

appDelegate设置的窗口和viewController在哪里?

查看设计器和上下文单击(XCode 4)行下面的app delegate对象。窗口和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.

_window iVar

它是自动的为你提供。我不清楚它是继承还是由编译器生成。

It's automatically provided for you. Not clear to me whether it's inherited or generated by the compiler.

这里有关于_ iVars的更多内容:
为什么要在iOS中重命名具有前导下划线的合成属性?

Here's more on _ iVars: Why rename synthesized properties in iOS with leading underscores?

如果您选择在代码中执行等效操作:

删除plist xib引用
main.m:传递名称委托

remove plist xib reference main.m: pass name of delegate

int main(int argc, char *argv[])
{
    int retVal = UIApplicationMain(argc, argv, nil, @"HelloViewAppDelegate");

在app delegate中:

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;
}

这篇关于您的Application Delegate设置在哪里以及谁初始化其window和viewController属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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