2017-02-04 3 views
0

私はこのコードを使ってウィンドウを構成しました。それは前に働いていた。とにかく、私がself.windowにアクセスするたびに、ウィンドウがペン先からロードされます。このことは再帰的になるため、ここでは問題になります。私は別のウィンドウを毎回取得するので、それは他の場所でも問題です! "のNSWindowController" から`self.window`にアクセスするたびにウィンドウがロードされる

enter image description here

/* The window getter will load the nib file (if there is one and it has not yet been loaded) and then return the window. 
If it has to load the window, it will first call -windowWillLoad, then -loadWindow, then -windowDidLoad. 
To affect nib loading or do something before or after it happens, you should always override those other methods. 

     The window setter is used internally from -initWithWindow: or when a controller's nib file is loaded (as the "window" outlet gets connected). 
You can also call it yourself if you want to create the window for a window controller lazily, but you aren't loading it from a nib. 
This can also be used to set the window to nil for cases where your subclass might not want to keep the window it loaded from a nib, but rather only wants the contents of the window. 
Setting the window to nil, after the nib has been loaded, does not reset the -isWindowLoaded state. 
A window controller will only load its nib file once. This method makes sure the window does not release when closed, and it sets the controller's -windowFrameAutosaveName onto the window and updates the window's dirty state to match the controller's document (if any). 
It also calls -setWindowController: on the window. You can override this if you need to know when the window gets set, but call super. 
    */ 
    @property (nullable, strong) NSWindow *window; 

答えて

1

あなたのウィンドウNIBにはどのようなオブジェクトでありますか?私はあなたがNIBにウィンドウコントローラクラスのインスタンスを作成したと思われます。

NIBを(おそらくコードで作成したウィンドウコントローラクラスのインスタンスを介して)ロードすると、ウィンドウコントローラの新しいインスタンスが作成されます。その新しいインスタンスは-awakeFromNibを受け取り、windowを要求します。これによりNIBの別のインスタンスがロードされ、プロセスが繰り返されます。

ウィンドウコントローラは、ウィンドウNIBでインスタンス化しないでください。 NIBのFile's Ownerプレースホルダは、ウィンドウコントローラのクラスを持つように構成する必要があります。ウィンドウコントローラはコードでインスタンス化し、NIBの所有者として自身を使用するように初期化する必要があります。そうすれば、File's Ownerがそれを保持している場所がいっぱいになります。

さらに、-awakeFromNibの上書きは避ける必要があります。予期せずに呼び出すことができます。これらの種類のタスクには、通常-windowDidLoadを上書きする方が安全です。

+0

ああ、それはまさにそれでした。私はウィンドウのNIBにコントローラオブジェクトを持っていました!ありがとうございました! – netigger

+0

待って、もう一度これを確認させてください。ちょうどそれを再作成しましたが、NIBにはまだオブジェクトがありません。 – netigger

+0

私は実際にはまだ問題がありました。しかし、私はまだawakeFromNibから 'self.window'にアクセスしていましたが、今でもうまくいきました。 – netigger

関連する問題