Connect Window Controller Outlet to Window

2019-03-20 17:29发布

问题:

I am trying to create a simple modal dialog in XCode 4. I have created the xib, created the NSWindowController derived class and made it the class in the File's Owner of the xib. The part I am having trouble with is physically connecting the window outlet as mentioned in the answers to questions:

How to give focus to NSWindow loaded from NIB?

Why do I have to call showWindow on my NSWindowController twice on 10.5?

and in Apple documentation here:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Documents/Concepts/WinControllersAndNibs.html

I understand the concept, but I cannot figure out how to actually do it in XCode 4! I know how to connect a control in a xib to an outlet or action in a class via CTRL-drag. I just do not know what to drag to and from in this case.

Any help appreciated.

回答1:

OK - I figured it out.

window outlet: In the IB Dock, select the File's Owner object. Control-drag from there to the window in the XIB file. That will create the new Outlet.

delegate outlet: To connect the delegate outlet to the File's Owner, select the Window in the IB Dock. In the connection inspector, under Outlets, control-drag from "delegate" to "File's Owner" This connection can also be made with the File's Owner selected, just control-drag from Referencing Outlets to the window and select "delegate" from the popup.

Edit: The best way to get this effect, delegate-to-File's Owner outlet in Window and window-to-Window outlet in File's Owner, is to create the NSWindowController-derived class first and let XCode create the xib in the process. Both outlets will be automatically created.

If the xib was created first, and you wish to hook it to a NSWindowController-derived class, then the outlets can be created via ctrl-drag in IB:

  • To create the window-to-Window outlet in File's Owner, select File's Owner and ctrl-drag from File's Owner to Window, then select "window" in the popup.
  • To create the delegate-to-File's Owner outlet in Window, select Window and
    • Ctrl-drag from Window to File's Owner, then select "delegate" in the popup.
    • or then select Connections Inspector and ctrl-drag from delegate to File's Owner.

These steps can also be "inverted" by using New Referencing Outlet....