0

OK、私は何が間違っているのですか?
1.次の名前のココアアプリとappDelegateを作成しました:window2AppDelegate
2. window2AppDelegate.h

#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. xibエディターで、ウィンドウコントローラーに接続されたウィンドウ-appdelegateに設定し、ボタンクリックアクションをボタン
4に設定します。

#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.コントローラーからウィンドウに接続されたPrefWindowwindowIBOutletという名前の新しいxibファイルを作成しました(コントローラーもPrefWindowControllerに設定されています)オプション「起動時に表示」チェックなし!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が表示されません!?なぜそして何が間違っているのですか?ココアのObjective-Cで他のウィンドウを表示することは、「愚かな」JavaやC#よりも難しいことを私に言わせないでください。

4

3 に答える 3

1

最後に、私は問題を管理しました!PrefWindow の nib エディターで、次のように設定する必要がありました。6 日間試行に失敗した後、Google は機能します。
とにかく、すべての応答と時間に感謝します!

于 2010-12-15T18:05:04.877 に答える
0

PrefWindowController の作成を applicationDidFinishLaunching に移動することをお勧めします。

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

于 2010-12-14T23:17:12.860 に答える
0
- (id) init {
   if ((self = [super init])) {
      ctrl = [[PrefWindowController alloc] init];
   if ([ctrl window] == nil)
      NSLog(@"Seems the window is nil!\n");
   }
   return self;
}

initIBOutlets をテストしようとするのは、物事の計画が早すぎます。彼らはまだnilです。オブジェクト作成プロセスの後半まで、ペン先アウトレットは「接続」されません。nib ファイル内のすべてが接続されていることを確認できる標準的な方法は次のとおりです。

- (void)awakeFromNib {

}

その時点で、すべての IBOutlets が有効である必要があります (まだアンロードされている別の nib 内のオブジェクトを意図的に参照していない場合)。

PrefWindowController が、ユーザーがアプリ メニューから [Preferences] を選択した後にのみ使用されるクラスである場合、私は他の意見に同意せず、初期ロード中に PrefsWindowController のインスタンスをまったく作成しないと言わざるを得ません。(メイン コントローラーは、設定ウィンドウから独立して機能できる必要があります)。設定ウィンドウをロードするためのメソッドがある場合、そのメソッドが呼び出されたときに、PrefsWindowController インスタンスが nil かどうかを確認し、nil の場合はインスタンスを作成してから、設定ウィンドウの表示に進みます。

于 2010-12-15T06:32:42.767 に答える