如何使用xib-file正确实现NSWindowController子类

时间:2014-01-30 09:28:28

标签: objective-c xcode xib nswindowcontroller

我正在尝试使用新的xib文件实现NSWindowController子类,我阅读了大量书籍,并研究了StackOverflow,但是没有提供的步骤使我的窗口显示,子类代码也没有被执行。新的xib文件将其File的Owner设置为“LogNavigatorController”,并且已经建立了与窗口及其内容的连接。

我的AppDelegate.h:

#import <Cocoa/Cocoa.h>

@class LogNavigatorWindowController;

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    LogNavigatorWindowController *logsWindowController;
}

@end

我的AppDelegate.m:

#import "AppDelegate.h"
#import "LogNavigatorWindowController.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application

    logsWindowController = [[LogNavigatorWindowController alloc] initWithWindowNibName:@"LogNavigatorWindowController"];
    [logsWindowController showWindow:self];
}

@end

我的LogNavigatorWindowController.h:

#import <Cocoa/Cocoa.h>

@interface LogNavigatorWindowController : NSWindowController
{
    NSArray *directoryList1;
    NSArray *directoryList2;
    NSMutableArray *directoryList;
    NSMutableArray *filePaths1;
    NSMutableArray *filePaths2;
}

@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTableView *logsTableView;
@property (unsafe_unretained) IBOutlet NSTextView *logsTextView;
@property (assign) IBOutlet NSArrayController *LogListController;
@property (retain) NSMutableArray *logsArray;

- (void) myDirectoryLogFunction;

@end

我的LogNavigatorController.m:

#import "LogNavigatorWindowController.h"

@interface LogNavigatorWindowController ()

@end

@implementation LogNavigatorWindowController

@synthesize logsTableView;
@synthesize logsTextView;
@synthesize window;

- (id)init
{
    self = [super initWithWindowNibName:@"LogNavigatorWindowController"];
    [self loadWindow];
    [self showWindow:@"Log Navigator"];
    [self.window makeKeyAndOrderFront:nil];

    if (self)
    {
        // Initialization code here.
        [self myDirectoryLogFunction];
    }

    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];

    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

- (void) myDirectoryLogFunction
{
    NSLog(@"Code execution test successful");
}

@end

1 个答案:

答案 0 :(得分:2)

您不需要创建window属性,因为它已经可用于NSWindowController子类。也许这会导致问题。

此外,您的init方法包含许多不属于那里的代码。删除

[self loadWindow];
[self showWindow:@"Log Navigator"];
[self.window makeKeyAndOrderFront:nil];

以及替换

self = [super initWithWindowNibName:@"LogNavigatorWindowController"];

self = [super init];

您可能想要删除init方法,因为在您的情况下您不需要它。

并移动

[self myDirectoryLogFunction];

到windowDidLoad方法。

同时总是检查是否调用了实例化窗口控制器的代码(在你的情况下来自app delegates didFinishLaunching :)。有时,如果您在原始项目中进行了太多更改并且意外删除了委托连接或类似项目,则有助于创建新项目并在那里进行测试。