美文网首页
view关联xib框架

view关联xib框架

作者: 杨大虾 | 来源:发表于2017-07-03 12:00 被阅读15次

    h文件:

    @protocol CommonViewDelegate <NSObject>
    @optional
    //
    @end
    @interface CommonView : UIView
    
    + (CommonView *)createViewWithDelegate:(id<CommonViewDelegate>)delegate;
    
    @end
    

    m文件

    //
    @property (assign, nonatomic) id<CommonViewDelegate>delegate;
    
    
    + (CommonView*)createViewWithDelegate:(id<CommonViewDelegate>)delegate
    
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CommonView" owner:self options:nil];
        if (topLevelObjects == nil || [topLevelObjects count] <= 0){
            return nil;
        }
        CommonView *view = (CommonView *)[topLevelObjects objectAtIndex:0];
        view.delegate = delegate;
        
        NSLog(@"-------1");
        return view;
    }
    

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CommonView" owner:self options:nil];这句会立马触发以下的方法:

    
    -(void)awakeFromNib
    {
        [super awakeFromNib];
        
        NSLog(@"---------2");
    }
    

    所以打印出来,先2后1


    另外别忘了delegate是指向这个view,不是File`s Owner


    还有就是,如果view 是用代码计算布局的,

    记得把Autoresizing去掉,否则会冲突

    相关文章

      网友评论

          本文标题:view关联xib框架

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