我有一个关于 IOS 应用程序的新手问题...如果我创建一个名为 TestForStackOverflow 的新的基于视图的应用程序,Xcode 会自动为 TestForStackOverflowAppDelegate.h 创建如下代码:
@class TestForStackOverflowViewController;
@interface TestForStackOverflowAppDelegate : NSObject <UIApplicationDelegate>
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet TestForStackOverflowViewController *viewController;
@end
以及 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 源文件仅包含
下面的代码:
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
难道不应该在 UIApplicationMain 函数调用的第四个参数中设置应用程序委托类吗?
2)TestForStackOverflowAppDelegate类的window和viewController属性在哪里设置?
3)这可能是微不足道的,但为什么我们要合成 window = _window,而在 TestForStackOverflowAppDelegate 接口中没有名为 _window 的实例变量?我发现您可以在类接口中声明 @properties 而不需要相应的 iVar(也许它们是由编译器自动创建的),但这是一个好的做法还是您应该始终在类中创建相应的 iVar?
请原谅我的信息很长,我只是希望我没有写出太明显的问题,因为在意大利已经是深夜了,我很累..但是当这些问题出现在我的脑海中时,我迫不及待解决方案 :)