搞不懂的xib-1_filesowner

搞不懂的xib-1_filesowner_第1张图片
image.png
搞不懂的xib-1_filesowner_第2张图片
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了,没拖拽线还好。
    //如果拖拽了线-》即设置了   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:
    //    >
    
    //    Printing description of self->_vaaa:
    //    >
    
    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,会造成死循环。

        
            
            
            
                
                    
                    
                
            
            
            
                
                
                
                
            
        

@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、 outlet property -----定义成员属性
3、 action selector ------定义方法
4、 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

     
     
     
     
    
     



        
            
                
                
                
                
                
            
        


        



        
            
            
            
                
            
            
            
            
                
                
            
        



        
        
        
        
            
            
            
            
                
                
            
        
        
        
            
            
            
                
            
        
        
        
            
            
            
                
            
        




        
            
            
            
            
        
        
            
            
            
            
        
        
            
                
            
        


        
            
            
            
            
            
                
                
                
                
            
        


        
            
                
            
        

        
        
        
    







    


    //File's Owner
        
            
            //IBOutlet
                
                
        

            
        



    // First Responder
        


    //UIVIEW
        

            
                
            

        //IBOutlet
        
                
            



    



    //SearchBar
        
        

    // UIButton
        


    // UISegmentedControl
    
        


    // UIPickerView
        
        


    










你可能感兴趣的:(搞不懂的xib-1_filesowner)