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.
发布评论
评论(1)
Interface Builder 中的每个设置在对象中都有相应的属性。例如,比较
NSTextField
/UITextField
的 Interface Builder 检查器和NSTextField
/UITextField
。IBOutlet
和 target/action 机制也没什么特别的:前者只是使用 Key-Valued Coding 将 Outlet 设置为对象,后者只是使用setTarget:
和NSControl
的setAction:
(或 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 ofNSTextField
/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 usessetTarget:
andsetAction:
ofNSControl
(or a similar method ofUIControl
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 whenMainMenu.nib
is loaded, that is.)There is an open source project called
nib2objc
which translates anib
/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 ofMainMenu.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.