Cocoa - 如何在 InterfaceBuilder 中绑定 NSView 的宽度?

发布于 2024-10-02 04:59:07 字数 347 浏览 0 评论 0原文

我正在尝试创建一个简单的应用程序,在自定义视图中绘制网格。 自定义视图大小是固定的(它不依赖于窗口的大小)。

自定义视图嵌入在滚动视图中,以便在滚动视图无法显示整个自定义视图时能够探索网格。

现在我想添加滑块来控制网格参数(nb raws,nb columns,tile width,tile height,...),这些参数影响自定义视图的大小。

作为一项实验,我试图将一个滑块的值绑定到我的自定义视图的宽度,但未能找到一种方法来做到这一点。

我该怎么做这种事情? 是否可以在 InterfaceBuilder 中做到这一点?我希望在绑定检查器窗口中找到宽度绑定,但奇怪的是它不在那里;-)

谢谢。

I'm trying to create a simple application that draws a grid in a custom view.
The custom view size is fixed (it doesn't depend on the size of the window).

The custom view is embedded in a scroll view to be able to explore the grid when the scroll view can't display the entire custom view.

Now i want to add sliders for controlling the grid parameters (nb raws, nb columns, tile width, tile height, ...), and these parameters influence the size of the custom view.

As an experiment, i'm trying to bind one slider's value to the width of my custom view but fail to find a way to do it.

How am i supposed to do this sort of things ?
Is it possible to do it in InterfaceBuilder ? I expected to find a width binding in Bindings Inspector Window but it's not there, curiously ;-)

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女 2024-10-09 04:59:07

您无法绑定普通 NSView 的宽度,并且绑定到视图的属性始终是一个坏主意。视图属性很少可观察到。而且,没有 width 属性;它是结构体的一个成员,是 frame 属性的值,您必须一次性设置或根本不设置该属性。

至于在自定义视图中公开绑定,您可以这样做,前提是保留属性 可观察(仅包含使用其设置器更改属性的值)。您需要在视图类的 initialize 方法中公开绑定,并且需要 编写 IBPlugin

另请参阅 Cocoa 绑定编程主题

You can't bind the width of a plain NSView, and binding to a property of a view is always a bad idea. View properties are seldom observable. Moreover, there is no width property; it's one member of the structure that is the value of the frame property, which you must set all at once or not at all.

As for exposing bindings in your custom view, you can do that, provided you keep the properties observable (which consists of little more than only changing the property's value using its setter). You'll need to expose the bindings in your view class's initialize method, and you'll need to write an IBPlugin.

See also the Cocoa Bindings Programming Topics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文