2010-12-14 5 views
0

OK、どうしたのですか?
1.名前の作成したココアのアプリとappDelegate: - appdelegateに設定し、buttonclickボタン
4にアクションXIBエディタ、ウィンドウコントローラに接続されたウィンドウ内のwindow2AppDelegate
2. window2AppDelegate.h新しいNSWindowアプリケーションからのミッションは不可能ですか?

#import "PrefWindowController.h" 
@interface window2AppDelegate : NSObject <NSApplicationDelegate> { 
    NSWindow *window; 
    PrefWindowController * ctrl; 
} 

@property (assign) IBOutlet NSWindow *window; 
- (IBAction) buttonClick:(id)sender; 
- (IBAction) buttonCloseClick:(id)sender; 
@end 


3 、

#import <Cocoa/Cocoa.h> 
@interface PrefWindowController : NSWindowController { 
@private 

} 
@end 

#import "PrefWindowController.h" 
@implementation PrefWindowController 

- (id)init { 
    self = [super initWithWindowNibName: @"PrefWindow"]; 
    return self; 
} 

- (void)dealloc { 
    // Clean-up code here. 
    [super dealloc]; 
} 

- (void)windowDidLoad { 
    [super windowDidLoad]; 
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. 
} 

@end 


5.(もPrefWに設定し、コントローラのコントローラからウィンドウに接続されているIBOutlet prefwindow要素のウィンドウという名前の新しいXIBファイルを作成した作成indowController)オプション "Visible At Launch"のオプションがUNCHECKED!私はこのウィンドウをbuttonclickで見たいと思う。
6.私が構築し、アプリケーションを実行した後window2AppDelegate

#import "window2AppDelegate.h" 
@implementation window2AppDelegate 
@synthesize window; 

- (id) init { 
    if ((self = [super init])) { 
     ctrl = [[PrefWindowController alloc] init]; 
    if ([ctrl window] == nil) 
     NSLog(@"Seems the window is nil!\n"); 
    } 
    return self; 
} 

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender { 
    return YES; 
} 

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

- (IBAction) buttonClick:(id)sender { 
// [[ctrl window] makeKeyAndOrderFront:self]; this doesn't work too :(
NSLog(@"it is here"); 
[ctrl showWindow:sender]; 
} 

- (IBAction) buttonCloseClick:(id)sender { 
    [window close]; 
} 

@end 


7を実装:closebuttonアプリが、buttonclickを閉じて - 私は表示されませんprefwindow要素!なぜ、何が間違っているのですか?ココアの目的別に別のウィンドウを表示することは、 "愚かな" JavaやC#よりも難しいですか?

+0

'-buttonClick:'に入るとき 'ctrl'が' nil'でないことを確認しましたか? –

+0

はい、initと同様ですが、同じ結果を返します。これはnilです – uniquepito

答えて

0

私はあなたがapplicationDidFinishLaunchingにPrefWindowControllerの作成を動かすことをお勧めしたい:

私はアプリケーションデリゲートのinitメソッドが呼び出されるかわかりません。おそらく、initWithCoder:は、オブジェクトをNIBからアーカイブ解除するときに呼び出されます。

+0

デリゲートがNIB内のカスタムオブジェクトである場合、そのinitが呼び出されるはずですが、applicationDidFinishLaunching内のPrefWindowControllerを作成するのがさらに良い形式です: – ughoavgfhw

0
- (id) init { 
    if ((self = [super init])) { 
     ctrl = [[PrefWindowController alloc] init]; 
    if ([ctrl window] == nil) 
     NSLog(@"Seems the window is nil!\n"); 
    } 
    return self; 
} 

initあまりにも早くIBOutletsをテストしようとしているように、物事の仕組みです。まだ彼らはまだゼロになるだろう。後でオブジェクト作成プロセスでは、ペン先が「接続」されるまではありません。あなたはnibファイルのすべてがフックアップされたことを知ることができる標準的な方法は次のとおりです。

- (void)awakeFromNib { 

} 

その時点で、あなたのIBOutletsのすべてが有効でなければなりません(彼らは意図的に別々のオブジェクトを参照していない提供、まだアンロードされていないペン先)。

PrefWindowControllerは、ユーザーがアプリメニューからPreferences]を選択しますた後、私は他の人に反対しなければならないと、私は中に全くPrefsWindowControllerのインスタンスを作成しないだろうと言うでしょう使用されるクラスである場合初期荷重。 (あなたのメインコントローラはprefsウィンドウから独立して機能するはずです)。そのメソッドが呼び出されるときに、の環境設定ウィンドウをロードする方法がある場合は、PrefsWindowControllerインスタンスがnilであるかどうかを確認し、そうであれば作成してから、prefsウィンドウを表示する必要があります。

+0

それはまだ私が間違って何かをしているようだif(ctrl == nil){ ctrl = [[PrefWindowController alloc] init]; if([ctrl window] == nil) NSLog(@ "ウィンドウが見えません!\ n"); } をbuttonClickメソッドに追加しましたが、出力に「stil」と表示されます。私はawakeFromNibの同じ行で同じ結果を返しました – uniquepito

1

最後に問題を管理しました。 PrefWindowのnibエディタで、私は次の操作を行う必要がありました。ファイルの所有者クラスをNSWindowControllerに設定します。次にウィンドウをIBOutletにファイルの所有者から私の(preferneces)ウィンドウに接続します。 6日間の試行が失敗した後、googleが動作します。
とにかく、あなたのすべての応答と時間をいただきありがとうございます!

+0

私はそれらの設定を再度確認することを提案していましたが、 "新しいxibファイルをPrefWindowウィンドウIBOutletという名前で作成しました。 PrefWindowControllerに設定されています) "PrefWindowControllerに設定されたコントローラーも" PrefWindowControllerになるようにFileのオーナークラスを設定していました。 – NSGod

関連する問題