为什么这个应用程序委托代码可以工作

发布于 2024-10-18 10:59:32 字数 1178 浏览 1 评论 0原文

刚刚开始 iPhone 开发。

我不确定为什么这段代码有效。我创建了一个新的 UI 控制器,然后将其详细信息添加到应用程序委托 *.h 文件和 *.m 文件中,然后在 IB 中将其连接起来。当我运行它时,事情就起作用了,所以我确实从我添加的控制器中看到了我的新视图...

但是我在应用程序委托中的 didFinishLaunchingWithOptions 方法中看到我从未真正创建过我的控制器? (即分配它/创建对象)。

怎么事情真的起作用了?

(A) *.m

#import "windowsBasedAppDelegate.h"
#import "gregsController.h"

@implementation windowsBasedAppDelegate

@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

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

    // Override point for customization after application launch.
    [self.window addSubview:viewController.view];

    [self.window makeKeyAndVisible];

    return YES;
}

(B) *.h

#import <UIKit/UIKit.h>
@class gregsController;

@interface windowsBasedAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    gregsController *viewController;

}

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

@end

谢谢

Just starting up with iPhone development.

I'm not sure why this code works. I've created a new UI controller, then added it's details to the application delegate *.h file and the *.m file, also then connected things up in IB. Things work when I run it, so that I do actually see my new view from the controller I added...

However I see in the didFinishLaunchingWithOptions method in the application delegate I never actually created my controller? (i.e. alloc'ed it / created the object).

How come things actually work?

(A) *.m

#import "windowsBasedAppDelegate.h"
#import "gregsController.h"

@implementation windowsBasedAppDelegate

@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

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

    // Override point for customization after application launch.
    [self.window addSubview:viewController.view];

    [self.window makeKeyAndVisible];

    return YES;
}

(B) *.h

#import <UIKit/UIKit.h>
@class gregsController;

@interface windowsBasedAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    gregsController *viewController;

}

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

@end

thanks

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

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

发布评论

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

评论(1

自在安然 2024-10-25 10:59:32

如果您在界面生成器中创建了一个对象,并连接了插座,那么 nib 文件实际上会使用 initWithCoder 为您实例化该对象:

If you created an object in interface builder, and connected the outlet, then the nib file actually instantiates the object for you, using initWithCoder:

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