美文网首页
搞不懂的xib-1_filesowner

搞不懂的xib-1_filesowner

作者: 我是小胡胡123 | 来源:发表于2017-09-07 22:52 被阅读89次
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>










相关文章

  • 搞不懂的xib-1_filesowner

    结论:nib存档-》ios类的实例1、nib存档可以通过loadNibNamed:owner:options方法解...

  • 如何才能做个硬核程序员,在这里给你三点建议,年薪百万不是梦

    一、内心建设 刚开始入门,最搞不懂的语法和算法,搞不懂C++的STL,搞不懂Java的Full GC,搞不懂函数式...

  • 做一个优秀的程序员到底难在哪里?

    初入此门,觉得语法和算法很难,搞不懂C++的STL,搞不懂Java的Full GC,搞不懂函数式,搞不懂动态规划和...

  • 做一个优秀的程序员到底难在哪里?

    初入此门,觉得语法和算法很难,搞不懂C++的STL,搞不懂Java的Full GC,搞不懂函数式,搞不懂动态规划和...

  • 搞不懂

    我就是想不通 那个另一半是不是知道你已经结婚啦,如果知道还继续让你跟它,它不是人品有问题嘛(肯定想着拆散一对是一对...

  • 搞不懂

    为什么我们会在打游戏的时候想尽办法过关,在现实生活中却不会? 我想做一个酷的人,在心爱的东西即将失去的时候依旧可以...

  • 搞不懂

    有一刻突然特别怕死 我想知道女娲创造人类是为了什么? 活着或者死去 一生追寻的是什么? 可能她当时也没想那么多 只...

  • 搞不懂

    这世界总是有这么多搞不懂的事情,付出那么多却总是得不到。人有时就是很奇怪,对一个东西,一件事如此执着,虽然知道结局...

  • 搞不懂

    早晨六点去上课 路上有每天都在的清洁工阿姨 重复一个 不知道重复了多久的动作 这时一对情侣和我擦肩而过 刚打扫过的...

  • 搞不懂

    不在意是你冷漠 care了是多管闲事 有些人真的很难搞懂 就这样吧,该谁谁 何苦为难自己不讨好呢

网友评论

      本文标题:搞不懂的xib-1_filesowner

      本文链接:https://www.haomeiwen.com/subject/whmwjxtx.html