我正在看几个示例代码库和教程,只是完全跌跌撞撞,这在很大程度上是因为这些教程是为Xcode < 4.2编写的,ARC更改了一些东西。
我正在尝试构建一个使用MVC设计模式的接口。我正在使用为Application Delegate提供的基本模板。我添加了一个继承自NSWindowController的名为MainWindowController的类。在界面生成器中,首先我删除了MainMenu nib文件中的窗口对象(因为我想将它放在一个单独的文件中)。我创建了一个名为MainWindow(.xib)的新接口,将文件所有者更改为MainWindowController,并将委托添加到对象列表中。现在,在这一点上,有些东西没有被点击。
我没有完全掌握为了让Delegate加载和启动窗口控制器,我需要如何实现或者需要实现什么。首先,我尝试将窗口中" delegate“的出口链接到实际的应用程序代理(称为AppDelegate),然后将Delegate类中的窗口出口链接到Interface Builder中的窗口。
我希望这个问题有一个答案,但如果有正确的文档来描述这个过程,我会更满意。我确定在MacDev上有什么东西,但我很难找到它。
以下是我正在使用的内容:
@class MainWindow;
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (strong) MainWindow *mainWindowController;
@property (assign) IBOutlet NSWindow *window;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
- (IBAction)saveAction:(id)sender;
@end..。
@implementation AppDelegate
@synthesize window;
@synthesize mainWindowController;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize managedObjectContext = __managedObjectContext;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
MainWindow *controller = [[MainWindow alloc] initWithWindowNibName:@"MainWindow"];
mainWindowController = controller;
// ... the rest handles the ManagedObject Models...迄今为止的解决方案:
@synthesize mainWindowController = _mainWindowController; // IBOutlet is linked in IB
//...
- (void) applicationDidFinishLaunching: (NSNotification *) aNotification
{
//... initialization of event handling etc...
if ( !_mainWindowController ) _mainWindowController = [[ MainWindowController alloc] initWithWindowNibName:@"MainWindow"];
[_mainWindowController showWindow: self];
// ...应用程序代理可以在主nib文件中管理NSWindow,也可以将该任务委托给控制器(NSWindowController),这是典型的基于文档的应用程序或MVC设计模式。Basic-info.plist指定的默认nib文件定义加载哪个nib文件。通常,默认的笔尖应该是主菜单,它也会加载代理。然后,委托应该通过applicationDidFinishLoading:或awakeFromNib:执行控制器的初始化,并根据委托和mvc设计模式继续适当的委托。
我遇到的主要问题是启动窗口,这是由showWindow完成的:这个问题的恶化源于一个非常旧的Mac项目的源代码,该项目使用过时的函数和方法来完成委托,并将我引向了错误的道路。感谢你的回答,它最终让我在正确的地方寻找正确的问题,并找到了正确的答案。
发布于 2011-12-16 04:15:30
您的问题是,在第二个xib "MainWindow“中,正在创建一个新的AppDelegate对象,该对象与"MainMenu”xib中的AppDelegate对象完全没有任何关系。每个xib中的对象都是真正的对象,它们被序列化,然后在运行时加载。
此特定问题将在资源管理指南"Nib Loading"中解决。我也可以推荐"Core Application Design"。
https://stackoverflow.com/questions/8525605
复制相似问题