为什么这个应用程序委托代码可以工作
刚刚开始 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在界面生成器中创建了一个对象,并连接了插座,那么 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: