我在哪里可以找到一本好书/资源来学习如何在 Cocoa 中以编程方式创建用户界面?

发布于 2024-11-29 02:46:37 字数 1459 浏览 0 评论 0原文

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

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

发布评论

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

评论(1

尐籹人 2024-12-06 02:46:37

Interface Builder 中的每个设置在对象中都有相应的属性。例如,比较 NSTextField/UITextField 的 Interface Builder 检查器和 NSTextField/UITextField

IBOutlet 和 target/action 机制也没什么特别的:前者只是使用 Key-Valued Coding 将 Outlet 设置为对象,后者只是使用 setTarget:NSControlsetAction: (或 iOS 上 UIControl 的类似方法。)

IB 所做的就是预煮这些对象,并将属性设置为您在检查器中指定。加载后,数据将被馈送到类的 initWithCoder: 中。

因此,如果您只是为了好玩而想要无 IB,您所要做的就是 alloc+init UI 对象,并通过以下方式设置所有属性代码。仅此而已(除了 OS X 上的特殊菜单项,它在加载 MainMenu.nib 时以某种方式隐式处理。)

有一个名为 nib2objc 翻译 nib/xib到一个带有显式 Cocoa 调用的 .m 文件。这将帮助您了解正在发生的事情。

有关 nib 文件的更多信息,请阅读 Apple 自己的文档本系列博客文章。但这实际上并不是关于如何在没有笔尖的情况下创建 UI;而是关于如何在没有笔尖的情况下创建 UI。这是关于系统如何处理特殊菜单项的。

无论如何,从内部来说,加载笔尖和以编程方式编写 UI 代码几乎没有区别;两者都只是创建 UI 对象并适当地设置 UI 属性。使用 IB,您可以预先设置属性,并在运行时读取预先准备的对象。如果您在代码中执行此操作,则属性将在运行时设置。但一旦设置完毕,生成的对象就完全相同。所以说,真的没有什么可学的。

Every setting in the Interface Builder has a corresponding property in the object. For example, compare the Interface Builder inspector for NSTextField/UITextField and the documentation of NSTextField/UITextField.

IBOutlet and the target/action mechanism are not special either: the former just uses Key-Valued Coding to set the outlet to an object, and the latter just uses setTarget: and setAction: of NSControl (or a similar method of UIControl on iOS.)

What the IB does is to precook these objects with the properties set as you specify in the inspector. When loaded, the data is fed to initWithCoder: of the class.

So, if you'd like to go IB-less just for fun, all you have to do is to alloc+init the UI object, and set all the properties by code. There's nothing more than that (except for special menu items on OS X which is somehow handled implicitly when MainMenu.nib is loaded, that is.)

There is an open source project called nib2objc which translates a nib/xib to a .m file with explicit Cocoa calls. That will help you understand what is going on.

On a bit more about nib files, read Apple's own documentation. The special magic at the loading of MainMenu.nib is described in this series of blog posts. But this is not really about how you would create UI without the nib in general; it's about how the special menu items are treated by the system.

In any case, internally speaking, there's almost no difference between loading a nib and writing UI codes programatically; both just create UI objects and set the UI properties appropriately. With IB, you set the properties beforehand and the precooked objects are read at the run time. If you do it in your code, the properties are set at run time. But once they are set, the resulting objects are exactly the same. So, there's really not much to learn.

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