0

I sub-classed NSWindow (named IMWindow) to make my own properties. Now I want to bind the value of NSTextField to one of the property of IMWindow. I know it is possible for NSUserDefaults controllers or app delegate. But it does not provide option for NSWindow.

I would like to see if I can bind the value to arbitrary object.

Screenshot:

enter image description here

Code:

@interface IMMainWindow ()
@property (nonatomic, strong) NSString *line1Text;
@end

It is hard to find any advance material in Interface Builder for OS X. :-/

HKTonyLee
  • 3,111
  • 23
  • 34

2 Answers2

2

Cocoa uses the MVC (Model-View-Controller) pattern. You should be binding the text field's value to a controller, not the window itself. Don't subclass NSWindow — create an NSWindowController subclass with a XIB (so the window controller will become File's Owner) and then add your line1Text property to the NSWindowController subclass and bind the text field to the File's Owner object in the XIB.

indragie
  • 18,002
  • 16
  • 95
  • 164
0

I agree with indragie's answer. That said, it is possible to do, what you are asking.

First you have to set the Custom Class of your IMWindow.xib. To do so, select the window in the left side and click on the blue highlighted icon in the right side and type in your class name: Xcode interface builder

When that is done, you have to create a property for your NSTextField this is done by selecting the text field and click the middle icon in the top (the one that has a bow and buttons). Select the blue icon highlighted on the right (the circle with an arrow). Now press the circle on the "New Reference Outlet"-line and drag the mouse into the header file of IMWindow. Xcode interface builder

That's it! Your NSTextField is now bound to the IMWindow class.

But remember: You should consider using a NSWindowController for this instead!

Community
  • 1
  • 1
ThomasCle
  • 6,792
  • 7
  • 41
  • 81
  • Thanks! I understand how to bind outlet but don't know about the value bindings (because I am proficient in iOS but new to OS X). Does it mean that I cannot bind to arbitrary object? Is there any limitation for the object? – HKTonyLee Feb 19 '14 at 07:45
  • Ah sorry man. I misunderstood your problem. Have you tried this: http://stackoverflow.com/a/14079680/1118969 ? I can see there are a lot of binding settings under `Value` if you select the second icon from right when `NSTextField` is selected. – ThomasCle Feb 19 '14 at 08:03