搞不懂的xib-1_filesowner

image.png
image.png
- (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>










最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353

推荐阅读更多精彩内容