encoderWithCoder:
method won't be called because the time when XCode encode the object graph into xib is not running time, it can't call your encoderWithCoder:
method.
How does Interface Builder serialize the object graph?
If you open Xib as source code, you can see it like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="4514" systemVersion="13B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none">
<dependencies>
<deployment defaultVersion="1536" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3747"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="EMXibViewController">
<connections>
<outlet property="view" destination="1" id="3"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="1">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" id="XyJ-CF-vRx">
<rect key="frame" x="68" y="102" width="204" height="403"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
</view>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/>
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina4"/>
</view>
</objects>
</document>
We can see xib only keeps the non-default value of object and the relationships between them.
The xib an UIViewController and has only two UI object , one is the UIViewController's view, the other is it's subview let's say it mysubview
and talks about how XCode archive mysubview
.
I have changed its backgroundColor、frame and autoresizingMask property of mysubview
, take backgroundColor
property as example, the backgroundColor property is saved as "color key="backgroundColor" white="0.0" alpha="1" colorSpace="calibratedWhite"". It will archive the backgroundColor of mysubview
.
Xib just archive the backgroundColor and autoresizingMask and frame property of the mysubview
object and when load the xib, it will call UIView's initWithCoder
method. In the method, it will get the UIColor object for the key @"backgroundColor" and CGRect for the key @"frame" and UIViewAutoresizing for the key @"autoresizingMask", any other property of UIView assigned default value.
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder] ;
UIColor * backgroundColor = [aDecoder decodeIntegerForKey:@"backgroundColor"] ;
if (backgroundColor) {
self.backgroundColor = backgroundColor ;
} else {
self.backgroundColor = $(defaultColor) ; // if no key in xib, use the default value.
}
// more...
return self ;
}
It's a nice question and make me think more.