2014-01-30 12 views
2

我想用新的xib文件實現NSWindowController子類,我讀了很多書,並在StackOverflow上進行了研究,但沒有提供任何步驟使我的窗口顯示,也沒有執行子類代碼。新的xib文件將其文件所有者設置爲「LogNavigatorController」,並已連接到窗口及其內容。如何使用xib文件正確實現NSWindowController子類

我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 
+0

'NSWindowController'已經有一個'window''屬性,所以我懷疑你提供的是自己的問題。 – trojanfoe

+0

Thanks @trojanfoe刪除了屬性及其實現代碼(綜合),仍然沒有骰子。 –

回答

2

您不需要創建窗口屬性,因爲它已經可用於NSWindowController子類。也許這會導致問題。

此外,您的init方法包含很多不屬於此處的代碼。刪除

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

以及與

self = [super init]; 

更換

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

你可能想在所有去除init方法,請你以後不要在你的情況需要它。

並移動

[self myDirectoryLogFunction]; 

到windowDidLoad方法。

也總是檢查實例化窗口控制器的代碼(你的情況從應用程序代表didFinishLaunching:)被調用。有時候,如果您在原始項目中發生了太多變化,並且意外刪除了委託連接或類似情況,那麼有助於創建新項目並在那裏進行測試。

+0

謝謝@Volker現在做了所有這些,但子類代碼仍未執行,並且窗口不顯示。 –

+0

我懷疑它與AppDelegate類有關,因爲它應該調用NSWindowController子類,因爲我讀它並且代碼原樣不執行。 –

+0

您是否在您的AppDelegate的initWithWindowNibName調用中檢查XIB名稱是否正確? – Volker