- (nullable NSArray *)loadNibNamed:(NSString *)name owner:(nullable id)owner options:(nullable NSDictionary *)options;
结论:
nib存档-》ios类的实例
1、nib存档可以通过loadNibNamed:owner:options方法解档
(1)给file's owner设置customClass,不会调用customClass类的initWithCoder/awakeFromNib方法。
注意:
//file's owner的customClass设置定为testview了,没拖拽线还好。
//如果拖拽了线-》即设置了 <connections><outlet property=属性。默认owner为nil,就会找不到对应key而崩溃了,setValue:forUndefinedKey:
//Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x6000000183f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.'
// 返回的实例v的类型并不是testview类型,而是UIView类型的。filesowner的连接线的key对应的value和loadNibNamed返回的第一个view是相同的实例.loadNibNamed返回的是一个数组,对应第几个对象,就是与之匹配的实例
// Printing description of v:
// <UIView: 0x7ffdf05167d0; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x60800022a1e0>>
// Printing description of self->_vaaa:
// <UIView: 0x7f9a46d06a30; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x608000239220>>
testview*v= [[[NSBundle mainBundle] loadNibNamed:@"testview" owner:self options:nil] lastObject];
v.frame=self.bounds;
[self addSubview:v];
(2)给view设置customClass,会调用对应customClass类的initWithCoder/awakeFromNib方法。
注意:
filesowner为uiviewcontroller,view的某一个子view设置customClass,在customClass的initWithCoder/awakeFromNib方法中,再次loadNibNamed:owner:options,会造成死循环。
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="J1A-O9-XGh" customClass="testview">
<rect key="frame" x="20" y="20" width="335" height="627"/>
<color key="backgroundColor" red="0.2588235438" green="0.75686275960000005" blue="0.96862745289999996" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="J1A-O9-XGh" firstAttribute="top" secondItem="i5M-Pr-FkT" secondAttribute="top" constant="20" id="ENk-Ei-7li"/>
<constraint firstItem="J1A-O9-XGh" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" constant="20" id="knh-Oh-W4c"/>
<constraint firstAttribute="trailing" secondItem="J1A-O9-XGh" secondAttribute="trailing" constant="20" id="lV0-3F-6Pp"/>
<constraint firstAttribute="bottom" secondItem="J1A-O9-XGh" secondAttribute="bottom" constant="20" id="wJY-Jz-Z9f"/>
</constraints>
</view>
@implementation testview
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
self= [super initWithCoder:aDecoder];
return self;
}
-(void)awakeFromNib{
[super awakeFromNib];
testview*v= [[[NSBundle mainBundle] loadNibNamed:@"testview" owner:nil options:nil] lastObject];
v.frame=self.bounds;
[self addSubview:v];
}
@end
2、如果nib的files owner属性是uiviewcontroller,可以通过uiviewcontroller的初始化方法解档
实例的成员属性, 包含3大类型:
1、placeholder placeholderIdentifier= IBFilesOwner
2、placeholder placeholderIdentifier= IBFirstResponder
3、view 去掉UI开头(1个或多个)
每一类型,又包括几个大属性
1、customClass
2、 <connections> outlet property -----定义成员属性
3、 <connections> action selector ------定义方法
4、 <connections> outletCollection property -------定义成员属性,1对多的关系.
@property(nullable, nonatomic,copy) NSArray<__kindof UIGestureRecognizer *> *gestureRecognizers
connections,连线 key-value-属性/方法。
1、Outlets
2、Outlet Collections
3、Referencing Outlets
4、Refercing Outlet Collection
5、Send Events
UIControl类和子类可以连接Send Events线
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
6、Received Actions
7、Send Actions
有这些属性的类:比如UIBarButtonItem,UIGestureRecognizer可以连Send Actions线。
SEL action; // default is NULL
id target; // default is nil
- (void)addTarget:(id)target action:(SEL)action;
注意:
不管是IBFilesOwner,还是IBFirstResponder,还是view,只要拖拽线了connections,即设置了outlet property, 那么,
IBFilesOwner:在loadnib的方法中,owner参数指定的customClass类型,就必须定义相对应的成员变量,以便kvc访问。如果不满足要求,crash:this class is not key value coding-compliant for the key view
IBFilesOwner id=-1
IBFirstResponder id=-2
view id="iN0-l3-epB"
swipeGestureRecognizer id=cJX-Ov-eYd
constraints
connections
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="view6testfileowner">
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB" customClass="test5view">
<swipeGestureRecognizer direction="right" id="cJX-Ov-eYd">
<constraints>
<connections>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="view6testfileowner">
<connections>
<outlet property="button22" destination="hSs-g6-Zcb" id="S44-re-23V"/>
<outletCollection property="gestureRecognizers" destination="bRc-6u-nqH" id="5Q3-aK-nb2"/>
<outletCollection property="gestureRecognizers" destination="sph-Wi-q3Y" id="cVe-Dw-KbL"/>
<outletCollection property="gestureRecognizers" destination="FTH-jQ-0KQ" id="kJD-l2-5tK"/>
<outletCollection property="gestureRecognizers" destination="Sao-Uu-2x8" id="Zw6-fE-i2v"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB" customClass="view6testcustomclass">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="hSs-g6-Zcb">
<rect key="frame" x="73" y="148" width="46" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
<connections>
<action selector="buttonevent22:" destination="-1" eventType="touchUpInside" id="loa-nQ-S4O"/>
<action selector="buttonevent:" destination="iN0-l3-epB" eventType="touchUpInside" id="D5i-jZ-skY"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<gestureRecognizers/>
<connections>
<outlet property="button" destination="hSs-g6-Zcb" id="fVD-gV-HSN"/>
<outletCollection property="gestureRecognizers" destination="bRc-6u-nqH" appends="YES" id="D11-6S-I5n"/>
</connections>
</view>
<barButtonItem style="plain" systemItem="flexibleSpace" id="0FQ-eX-OQQ"/>
<barButtonItem width="42" style="plain" systemItem="fixedSpace" id="vJq-tU-0gK"/>
<tabBarItem title="Item" id="rna-el-Cdi"/>
<tabBar contentMode="scaleToFill" id="gQC-ip-Xmd">
<rect key="frame" x="0.0" y="0.0" width="375" height="49"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<items>
<tabBarItem systemItem="favorites" id="O02-mz-Jbc"/>
<tabBarItem systemItem="more" id="hoa-y7-BYy"/>
</items>
</tabBar>
<barButtonItem title="Item" id="eKi-Gc-e9E"/>
<toolbar opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="Et1-hk-hZH">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<items>
<barButtonItem title="Item" id="vT4-Tn-HLf"/>
</items>
</toolbar>
<navigationItem title="Title" id="Iw7-eB-ytu"/>
<navigationBar contentMode="scaleToFill" id="PYX-Qq-D8N">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<items>
<navigationItem title="Title" id="M6H-XR-1a7"/>
</items>
</navigationBar>
<searchBar contentMode="redraw" id="2za-0g-Ttm">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<textInputTraits key="textInputTraits"/>
<point key="canvasLocation" x="568" y="-217"/>
</searchBar>
<searchBar contentMode="redraw" id="AMc-IM-pQb">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<textInputTraits key="textInputTraits"/>
<point key="canvasLocation" x="425" y="-67"/>
</searchBar>
<gestureRecognizer id="bRc-6u-nqH">
<connections>
<action selector="gestevent22:" destination="-1" id="peD-M2-Ept"/>
</connections>
</gestureRecognizer>
<view contentMode="scaleToFill" id="1hW-Yk-0BL">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<gestureRecognizers/>
<connections>
<outletCollection property="gestureRecognizers" destination="sph-Wi-q3Y" appends="YES" id="oR8-np-f49"/>
<outletCollection property="gestureRecognizers" destination="FTH-jQ-0KQ" appends="YES" id="vjy-od-J0d"/>
<outletCollection property="gestureRecognizers" destination="Sao-Uu-2x8" appends="YES" id="wk2-40-Ae8"/>
<outletCollection property="gestureRecognizers" destination="FZU-4h-HXw" appends="YES" id="vdM-NG-wwZ"/>
</connections>
</view>
<pinchGestureRecognizer id="sph-Wi-q3Y">
<connections>
<action selector="pinchevent22:" destination="-1" id="T8Z-pi-4hq"/>
</connections>
</pinchGestureRecognizer>
<rotationGestureRecognizer id="FTH-jQ-0KQ"/>
<pongPressGestureRecognizer allowableMovement="10" minimumPressDuration="0.5" id="FZU-4h-HXw"/>
<gestureRecognizer id="Sao-Uu-2x8"/>
</objects>
<objects>
//File's Owner
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="testViewController">
<connections>
//IBOutlet
<outlet property="VV" destination="i5M-Pr-FkT" id="GQi-Rt-mNN"/>
<outlet property="ADS" destination="w1S-FW-ofv" id="wDM-cS-FIh"/>
<outlet property="view" destination="i5M-Pr-FkT" id="1Pl-Eo-Flj"/>
</connections>
</placeholder>
// First Responder
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
//UIVIEW
<view contentMode="scaleToFill" id="iN0-l3-epB" customClass="HELLVIEW">
<subviews>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="w1S-FW-ofv">
<rect key="frame" x="116" y="96" width="46" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
</button>
</subviews>
//IBOutlet
<connections>
<outlet property="AA" destination="WB4-Nq-gB0" id="n7p-H0-blE"/>
</connections>
</VIEW>
//SearchBar
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT" customClass="UISearchBar">
</view>
// UIButton
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="aYl-Dr-B0q">
</button>
// UISegmentedControl
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" id="BrO-Gg-KJl">
</segmentedControl>
// UIPickerView
<pickerView contentMode="scaleToFill" id="P4x-er-kUd">
</pickerView>
</objects>
网友评论