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去掉,否则会冲突
网友评论