首页 > 编程知识 正文

cocoa表情包,cocoa

时间:2023-05-04 23:48:52 阅读:220617 作者:4107

场景

在用xcode开发Cocoa程序时, 我们一般会使用IB来设计界面xib. 而在xib文件里我们往往会对用到的自定义NSView进行绑定以便引用使用. 但是这些xib里的object是什么时候进行实例化并可用的?有没有消息通知.

有没有一个方法在xib界面对象加载完, 绑定完,在显示界面前能通知一下呢?答案是有的.

说明

xib文件在打包进入.app之后会编译为二进制格式的nib. Objective-c为NSObject添加一个非正式协议.这个协议只有一个方法awakeFromNib.
如果我们想让nib里的对象在被加载后通知,那么我们需要在接收者receiver里实现这个方法。

@interface NSObject (NSNibAwaking)- (void)awakeFromNib; /* On Mac OS X 10.6 and later, NSObject provides an implementation of awakeFromNib. This means that you can safely call through to [super awakeFromNib] in an overridden implementation when running on Mac OS X 10.6 and later. */@end

下面我们来看看这个方法的说明:

An awakeFromNib message is sent to each object loaded from the archive, but only if it can respond to the message, and only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet instance variables set. 一个awakeFromNib消息会发送给从nib里加载的每个对象。条件一是接收者能响应这个消息.条件二是所有在nib里已经被加载和初始化.当一个对象接收到awakeFromNib消息, 它的IBOutlet实例被保证会被全部设置. During the instantiation process, each object in the archive is unarchived and then initialized with the method befitting its type. Cocoa views (and custom views that can be customized using an associated Interface Builder palette) are initialized using their initWithCoder: method. Custom views are initialized using their initWithFrame: method. Custom classes that have been instantiated in the nib are initialized using their init method.

在实例化过程中, 每个在nib里的对象会被解包并且根据它们的类型进行初始化。

Cocoa views(并且那些通过IB面板进行自定义的的view)初始化使用initWithCoder:方法.自定义的类(自定义NSView子类)通过initWithFrame:方法进行初始化。自定义的类(不是NSView子类)通过调用它们的init方法进行初始化. Once all objects have been instantiated and initialized from the archive, the nib loading code attempts to reestablish the connections between each object’s outlets and the corresponding target objects. If your custom objects have outlets, an NSNib object attempts to reestablish any connections you created in Interface Builder. It starts by trying to establish the connections using your object’s own methods first. For each outlet that needs a connection, the NSNib object looks for a method of the form setOutletName: in your object. If that method exists, the NSNib object calls it, passing the target object as a parameter. If you did not define a setter method with that exact name, the NSNib object searches the object for an instance variable (of type IBOutlet id) with the corresponding outlet name and tries to set its value directly. If an instance variable with the correct name cannot be found, initialization of that connection does not occur. Finally, after all the objects are fully initialized, each receives an awakeFromNib message.

一旦所有在nib里的对象被实例化和初始化, nib加载代码会尝试做以下的操作:

尝试建立每个对象的IBOutlet和相应的target对象.NSNib对象会首先尝试使用自定义对象的方法setOutletName进行设置. 如果没有自定义的setter方法,会直接赋值设置.当所有的对象被初始化,每个对象都会接收到一个awakeFromNib消息. Because the order in which objects are instantiated from an archive is not guaranteed, your initialization methods should not send messages to other objects in the hierarchy. Messages to other objects can be sent safely from within awakeFromNib — by which time it’s assured that all the objects are unarchived and initialized (though not necessarily awakened, of course).

注意:

在nib里的对象实例化顺序是随机的,你的初始化方法不应该对所在层级里的其他对象发送消息。(因为其他对象可能没实例化,为nil).安全的发送消息给其他对象只有在awakeFromNib方法里, 因为在调用这个方法时, 它能保证所有对象已经被正确解包和初始化.

以下是正确使用awakeFromNib的例子, 进行相对位置的移动.

- (void)awakeFromNib { NSRect viewFrame; viewFrame = [firstView frame]; viewFrame.origin.x += viewFrame.size.width; [secondView setFrame:viewFrame]; return;} It is recommended that you maintain a one-to-one correspondence between your File’s Owner objects and their associated nib files. Loading two nib files with the same File’s Owner object causes that object’s awakeFromNib method being called twice, which could cause some data structures to be reinitialized in undesired ways. It is also recommended that you avoid loading other nib files from your awakeFromNib method implementation. 推荐你应该维护一对一的File Owner对应关联的nib文件, 因为两个nib文件使用同一个File Owner会导致这个Owner的awakeFromNib会被调用两次.推荐在awakeFromNib里回避加载其他的nib文件. 参考

NSNibAwaking protocol reference

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。