view关联xib框架

h文件:

@protocol CommonViewDelegate 
@optional
//
@end
@interface CommonView : UIView

+ (CommonView *)createViewWithDelegate:(id)delegate;

@end

m文件

//
@property (assign, nonatomic) iddelegate;


+ (CommonView*)createViewWithDelegate:(id)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


view关联xib框架_第1张图片

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


view关联xib框架_第2张图片

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

view关联xib框架_第3张图片

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

你可能感兴趣的:(view关联xib框架)