EricD
EricD

Reputation: 551

How to bind CGSize.width in cocoa (Interface Builder)?

I tried to bind the width value of a CGSize property of a Cocoa application with interface builder.

I set the "model key path" to "windowSize" (my CGSize value) and tried different values in the "Value Transformer field" like "width", or "%{width}@" but when I run the application, it crash with this error "Cannot find value transformer with name {width}@"

Which transformer should i use ?

Upvotes: 0

Views: 292

Answers (1)

Peter Hosey
Peter Hosey

Reputation: 96333

You'll need to make custom accessors in some object of yours—e.g., the window controller (which will have to be of a custom subclass of NSWindowController)—and bind to those.

In each accessor, get the window's frame or contentSize, and either retrieve or change the relevant member of it. In the setter, you then set the adjusted frame or size back where you got it; in the getter, you return the extracted number.

Binding directly to the width or height of a CGSize value, or to (any part of) the origin or size of a rectangle, won't work for the reason CodaFi described: You can only bind to properties of objects, and sizes and rectangles (and points) aren't objects.

Although you use the same “dot syntax” (foo.bar) for both, there is a distinction between accessing a member of a structure (such as the width of an NSSize or a CGSize) and accessing a property of an object (such as the contentSize of an NSWindow). You can bind to a property of an object; you cannot bind to a member of a structure.

Upvotes: 1

Related Questions