码迷,mamicode.com
首页 > 其他好文 > 详细

- awakeFromNib

时间:2015-09-22 16:12:39      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:


- awakeFromNib
Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.

Declaration
SWIFT
func awakeFromNib()
OBJECTIVE-C
- (void)awakeFromNib
Discussion
The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but 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 and action connections already established.

You must call the super implementation of awakeFromNib to give parent classes the opportunity to perform any additional initialization they require. Although the default implementation of this method does nothing, many UIKit classes provide non-empty implementations. You may call the super implementation at any point during your own awakeFromNib method.

NOTE
During Interface Builder’s test mode, this message is also sent to objects instantiated from loaded Interface Builder plug-ins. Because plug-ins link against the framework containing the object definition code, Interface Builder is able to call their awakeFromNib method when present. The same is not true for custom objects that you create for your Xcode projects. Interface Builder knows only about the defined outlets and actions of those objects; it does not have access to the actual code for them.

During the instantiation process, each object in the archive is unarchived and then initialized with the method befitting its type. Objects that conform to the NSCoding protocol (including all subclasses of UIView and UIViewController) are initialized using their initWithCoder: method. All objects that do not conform to the NSCoding protocol are initialized using their init method. After all objects have been instantiated and initialized, the nib-loading code reestablishes the outlet and action connections for all of those objects. It then calls the awakeFromNib method of the objects. For more detailed information about the steps followed during the nib-loading process, see Nib Files in Resource Programming Guide.

IMPORTANT
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 an awakeFromNib method.

Typically, you implement awakeFromNib for objects that require additional set up that cannot be done at design time. For example, you might use this method to customize the default configuration of any controls to match user preferences or the values in other controls. You might also use it to restore individual controls to some previous state of your application.

Import Statement
OBJECTIVE-C
@import UIKit;
SWIFT
import UIKit
Availability
Available in iOS 2.0 and later.
See Also
awakeAfterUsingCoder: (NSObject class)
initWithCoder: (NSCoding protocol)
initialize (NSObject class)

Responding to Size Change Events
- increaseSize:
Asks the object to increase the size of an item.

Declaration
SWIFT
func increaseSize(_ sender: AnyObject?)
OBJECTIVE-C
- (void)increaseSize:(id)sender
Discussion
The system calls this action method in response to the user pressing Command-plus (+) on an attached hardware keyboard. Typical responses for this type of event are to increase the font size of text or to change the zoom level of scroll views.

Import Statement
OBJECTIVE-C
@import UIKit;
SWIFT
import UIKit

awakeFromNib

在使用IB的时候才会涉及到此方法的使用,当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的awakeFromNib函数来响应这个消息,执行一些必要的操作。

awakeFromNib与ViewDidLoad的区别

         当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的 awakeFromNib函数来响应这个消息,执行一些必要的操作。也就是说通过nib文件创建view对象是执行awakeFromNib 。

viewDidLoad

当view对象被加载到内存是就会执行viewDidLoad,所以不管通过nib文件还是代码的方式创建对象都会执行viewDidLoad。

- awakeFromNib

标签:

原文地址:http://www.cnblogs.com/wenios/p/4829047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!