这是我从已经可以处理父窗口和工作表的应用程序中获取的摘要。这里的摘要编译,启动和显示父窗口,而没有收到我发送到其文本字段的消息。该运行为文本字段,按钮和窗口本身生成“缺少setter或实例变量”日志消息。由于某种原因,我没有得到正确初始化的父窗口,我怀疑我将在工作表窗口中遇到同样的问题,但我无法通过此问题来调试应用程序的其余部分。
我相信在将窗口连接到文件所有者的过程中省略了一些基本的东西,即使在查看.xib
的连接检查器时它显示自定义类是ParentClass和各种连接制作完成。我在Apple的迷宫文档中没有找到任何说明,也没有在StackOverflow中找到指导我完成连接过程,这将允许我发现我缺少的部分。
你会提供任何你能提供的东西。
ParentDelegate.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
@class ParentClass;
@interface ParentDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow * window;
@property (weak, nonatomic) ParentClass * parentController;
@end
ParentDelegate.m
#import "ParentDelegate.h"
@implementation ParentDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { }
- (void)applicationDidBecomeActive:(NSNotification *)aNotification { }
- (void)applicationDidResignActive:(NSNotification *)aNotification { }
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{ return YES; }
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{ return NSTerminateNow; }
@end
ParentClass.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
@interface ParentClass : NSWindowController {
}
@property (assign) IBOutlet NSWindow * window;
@property IBOutlet NSTextField * messageTextField;
@property IBOutlet NSButton * proceedButton;
@property (strong) NSMutableString * parentPropInfo;
- (IBAction) awakeFromNib;
- (IBAction) doProceed:(id)sender;
@end
ParentClass.m
#import "ParentClass.h"
#import "ParentDelegate.h"
#import "SheetClass.h"
@implementation ParentClass
ParentDelegate * parentDelegate;
SheetClass * sheetController;
- (IBAction)awakeFromNib {
parentDelegate = [NSApplication sharedApplication].delegate;
parentDelegate.parentController = self;
sheetController = [[SheetClass alloc] initWithWindowNibName: @"SheetClass"];
_messageTextField.stringValue = @"Click Proceed button";
}
- (IBAction)doProceed:(id)sender {
_parentPropInfo = @"Hello!".mutableCopy;
[NSApp runModalForWindow:sheetController.window];
// Sheet active now until it issues endModal, then:
_messageTextField.stringValue = sheetController.sheetPropInfo;
[NSApp endSheet: sheetController.window];
[sheetController.window orderOut:self];
}
@end
SheetClass.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
@interface SheetClass : NSWindowController {
}
@property (assign) IBOutlet NSWindow * window;
@property (weak) IBOutlet NSTextField * propTextField;
@property (weak) IBOutlet NSButton * returnButton;
@property (strong) NSMutableString * sheetPropInfo;
- (IBAction)awakeFromNib;
- (IBAction)doReturn:(id)sender;
@end
SheetClass.m
#import "SheetClass.h"
#import "ParentClass.h"
@implementation SheetClass
ParentClass * parent;
- (IBAction)awakeFromNib {
parent.window = self.window.sheetParent;
_propTextField.stringValue = parent.parentPropInfo;
}
- (IBAction)doReturn:(id)sender {
_sheetPropInfo = @"Done!".mutableCopy;
[NSApp stopModal];
}
@end
最终我想将这个迷你应用程序用作其他几个应用程序的起始模板。