【发布时间】:2011-11-24 00:53:13
【问题描述】:
我有一个关于 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,而在 TestForStackOverflowAppDelegate 接口中没有一个名为 _window 的实例变量?我已经看到您可以声明 @properties 而无需在类接口中使用相应的 iVar(也许它们是由编译器自动创建的),但这是一种好习惯还是应该始终在您的类中创建相应的 iVar?
对不起,很长的信息,我只是希望我没有写一个太明显的问题,因为在意大利是深夜,我很累..但是当这些问题出现在我脑海中时,我可以'不要等待解决方案:)
【问题讨论】:
标签: objective-c ios xcode uiapplicationdelegate